简体   繁体   中英

AttributeError: 'Series' object has no attribute 'days'

I have a column 'delta' in a dataframe dtype: timedelta64[ns], calculated by subcontracting one date from another. I am trying to return the number of days as a float by using this code:

from datetime import datetime
from datetime import date
df['days'] = float(df['delta'].days)

but I receive this error:

AttributeError: 'Series' object has no attribute 'days'

Any ideas why?

While subtracting the dates you should use the following code.

df = pd.DataFrame([ pd.Timestamp('20010101'), pd.Timestamp('20040605') ])
(df.loc[0]-df.loc[1]).astype('timedelta64[D]')

So basically use .astype('timedelta64[D]') on the subtracted column.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM