Home > Mobile >  apply on date difference returns NaT
apply on date difference returns NaT

Time:02-15

I'm trying to add a negative day to all the days in a list

def datediff(xdate):
  return(xdate - timedelta(days=1))

data_ = [['tom', '2022-12-01'], ['nick', '2022-12-01'], ['juli', '2021-06-08']]
data_ = pd.DataFrame(data_, columns = ['Name', 'eventdate'])

pd.DataFrame(index=data_.eventdate, data=data_, columns=['eventdate']).apply(datediff)   

However, the eventdate, that should contain the original date minus 1, is all NaT.

CodePudding user response:

Use a pd.Timedelta to do date manipulations in a DataFrame.

In [47]: data_ = [['tom', '2022-12-01'], ['nick', '2022-12-01'], ['juli', '2021-06-08']]
    ...: data = pd.DataFrame(data_, columns = ['Name', 'eventdate'])

In [48]: data
Out[48]:
   Name   eventdate
0   tom  2022-12-01
1  nick  2022-12-01
2  juli  2021-06-08

# data.eventdate is a string so we convert it to a datetime before subtracting a timedelta
In [49]: data.eventdate = data.eventdate.astype('datetime64') - pd.Timedelta('1 day')

In [50]: data
Out[50]:
   Name  eventdate
0   tom 2022-11-30
1  nick 2022-11-30
2  juli 2021-06-07

You should typically avoid using Dataframe.apply when you can. See this answer as to why it can be quite slow.

  • Related