简体   繁体   中英

Why am I getting error 'Series' object has no attribute 'days' when trying to get number of days between two dates?

I have come across some examples that show how to calculate the difference between two dates using.days. However for some reason does not seem to be working for me. I have the following code:

import datetime
from datetime import date
dfPort = pd.read_csv('C:\\Research\\Lockup\\Data\\lockupdates.csv')
dfPort = pd.DataFrame(dfPort)
todaysDate=datetime.datetime.today()
dfPort['LDate']=pd.to_datetime(dfPort['LockupExpDate'], format='%m/%d/%Y')
dfPort['TimeLeft']=(todaysDate-dfPort['LDate']).days

I get the following error:

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

So I tried the following:

xx=dfPort['LDate']-todaysDate
xx.days

and got the same error message. The references in Stack Overflow that I was reading are: Difference between two dates in Python

How to fix?

Your problem is because you are trying to access the days attribute on a Series Object. The result of subtracting the Series dfPort["LDate"] from todaysDate is a series of timedelta objects.
Instead, you could call the apply method of the series where you can access each member of the series like this:

dfPort['TimeLeft'] = (todaysDate-dfPort['LDate']).apply(lambda x: x.days)

That way, you are accessing the "days" attribute on each individual timedelta object within the series and the result of the apply function will return a series of the day values as integers.

I often use the Series.apply() method for small and even moderate-sized data sets. If you have a very large data set, then you might want to try something else that might not have the performance downsides of apply(). Here is a list comprehension approach, although I haven't tested to see how much better it might be:

dfPort['TimeLeft'] = [v.days() for v in (todaysDate-dfPort['LDate'])]

Not sure if there are other more-performant ways, but these should work.

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