Reputation: 23
I have a problem using np.select in DateTime series. Imagine you have a DataFrame: dateframe['id','FLOAT','DATE1','DATE2']
dateframe['DATE1'] = dt.datetime.now()
dateframe['DATE2'] = dt.datetime.now()
dateframe['DATE3'] = np.select(
[
dateframe['FLOAT']>0,
dateframe['FLOAT']<=0,
],
[
dateframe['DATE1'],
dateframe['DATE2'],
]
)
I get this error:
TypeError: The DTypes <class 'numpy.dtype[uint8]'> and <class 'numpy.dtype[datetime64]'> do not have a common DType. For example they cannot be stored in a single array unless the dtype is object
.
Thanks in advance.
Upvotes: 2
Views: 791
Reputation: 16683
use np.where
when you have one condition that creates exactly two mutually exclusive groups:
dataframe['DATE1'] = dt.datetime.now()
dataframe['DATE2'] = dt.datetime.now()
dataframe['DATE3'] = np.where(dataframe['FLOAT']>0,dataframe['DATE1'],dataframe['DATE2'])
dataframe
Regarding the error with np.select
. Initially store the date as an object datatype with str()
and then convert to_datetime()
later. Per the error, there is an issue with the datetime data type in the np.select statement
dataframe['DATE1'] = str(dt.datetime.now())
dataframe['DATE2'] = str(dt.datetime.now())
dataframe['DATE3'] = pd.to_datetime(np.select([(dataframe['FLOAT']>0),(dataframe['FLOAT']<=0)],
[dataframe['DATE1'],dataframe['DATE2']]))
dataframe
Upvotes: 2