This is my data:
date = df['Date']
print (date.head())
0 2015-01-02
1 2015-01-02
2 2015-01-02
3 2015-01-02
4 2015-01-02
Name: Date, dtype: datetime64[ns]
my code:
def date_to_days(date):
return date2num(datetime.datetime.strptime(date, '%Y-%m-%d'))
Why am I getting that error?
It works fine for me without any errors.
In [74]: from matplotlib.dates import date2num
In [75]: df['Number of days'] = df['Date'].apply(lambda x: date2num(datetime.datetime.strptime(x, '%Y-%m-%d')))
In [76]: df
Out[76]:
Date Number of days
0 2015-01-02 735600.0
1 2015-01-02 735600.0
2 2015-01-02 735600.0
3 2015-01-02 735600.0
4 2015-01-02 735600.0
In general, it's a bad practice to assign variables to a pandas series object. It can mess a lot of things up.
In [1]: def date_to_days(date):
...: return date2num(datetime.datetime.strptime(date, '%Y-%m-%d'))
In [2]: df['Number of days'] = df['Date'].apply(date_to_days)
In [3]: df
Out[3]:
Date Number of days
0 2015-01-02 735600.0
1 2015-01-02 735600.0
2 2015-01-02 735600.0
3 2015-01-02 735600.0
4 2015-01-02 735600.0
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.