I have a dataframe with a datetime column. I want to apply a function to set value as None in this column if the date is inferior to an other date. But the applied fonction set all my values to None. Could you help me ?
Here my code :
dateused = datetime.datetime.strptime('202004', '%Y%m')
df['date_pack'] =df['date_pack'].apply(lambda x: None if x < dateused else x)
The dtype of df['date_pack'] is datetime64[ns]. After this, all my values in my column 'date_pack' are None.
Thanks
I think you need Series.mask
with set values to NaT
for misisng values for datetimes:
df = pd.DataFrame({'date_pack': pd.to_datetime(['2020-08-10','2002-02-09'])})
dateused = datetime.datetime.strptime('202004', '%Y%m')
df['date_pack'] = df['date_pack'].mask(df['date_pack'] < dateused)
print (df)
date_pack
0 2020-08-10
1 NaT
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.