Home > Software design >  Trying to find the difference in days between 2 dates
Trying to find the difference in days between 2 dates

Time:04-09

I have a date column in my dataframe and I am trying to create a new column ('delta_days') that has the difference (in days) between the current row and the previous row.

# Find amount of days difference between dates
for i in df:
    new_date = date(df.iloc[i,'date'])
    old_date = date(df.iloc[i-1,'date']) if i > 0 else date(df.iloc[0, 'date'])
    df.iloc[i,'delta_days'] = new_date - old_date

I am using an iloc because I want to directly reference the 'date' column while i repersents the current row.

I am getting this error:

ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types

can someone please help

CodePudding user response:

You can use pandas.DataFrame.shift method to achieve what you need.

Something more or less like this:

df['prev_date'] = df['date'].shift(1)
df['delta_days'] = df['date'] - df['prev_date']
  • Related