这是我的数据:
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]
我的代码:
def date_to_days(date):
return date2num(datetime.datetime.strptime(date, '%Y-%m-%d'))
为什么我会犯那个错误?
它对我来说很好,没有任何错误。
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
通常,将变量分配给pandas系列对象是一种糟糕的做法。它会把很多事情搞砸。
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