Leere Datumswerte können nicht mit Numpy Nan gefüllt werdenPython

Python-Programme
Anonymous
 Leere Datumswerte können nicht mit Numpy Nan gefüllt werden

Post by Anonymous »

Ich habe den folgenden Code

Code: Select all

import pandas as pd
import numpy as np
dat = pd.DataFrame({'A' : [1,2,3,4,5], 'B' : ['2002-01-01', '2003-01-01', '2004-01-01', '2004-01-01', '2005-01-01']})
dat['B'] = pd.to_datetime(dat['B'])
dat['A'] = np.where(
dat['A'].isin([1,2]),
(dat['B'] + pd.DateOffset(months = 12)),
np.where(
dat['A'].isin([3,4]),
(dat['B'] + pd.DateOffset(months = 10)),
np.nan))
Grundsätzlich möchte ich die leeren Datumsfelder mit np.nan füllen. Mit dem obigen Code erhalte ich jedoch die folgende Fehlermeldung

Code: Select all

Traceback (most recent call last):
File "", line 4, in 
numpy.exceptions.DTypePromotionError: The DType  could not be promoted by . This means that no common DType exists for the given inputs. For example they cannot be stored in a single array unless the dtype is `object`. The full list of DTypes is: (, )
Könnten Sie bitte helfen, diesen Fehler zu beheben?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post