8

I have a pandas dataframe, 'df', where there is an original column with dates in datetime format. I set a hard date as a variable:

 hard_date = datetime.date(2013, 5, 2)

I then created a new column in my df with the difference between the values in the date column and the hard_date...

df['days_from'] = df['date'] - hard_date

This produced a good output. for instance, when I print the first cell in the new column it shows:

print (df['days_from'].iloc[0])

28 days 00:00:00

But now I want to convert the new column to just the number of days as an integer. I thought about just taking the first 2 characters, but many of the values are negative, so I am seeking a better route.

Any thoughts on an efficient way to convert the column to just the integer of the days?

Thanks

Jeff Saltfist
  • 933
  • 3
  • 15
  • 30
  • have you tried `.days`? datetime [timedeltas](https://docs.python.org/2/library/datetime.html#timedelta-objects) let you look at each piece separately. – TemporalWolf Feb 13 '17 at 23:26
  • Possible duplicate of [Convert a timedelta to days, hours and minutes](http://stackoverflow.com/questions/2119472/convert-a-timedelta-to-days-hours-and-minutes) – TemporalWolf Feb 13 '17 at 23:28

1 Answers1

9

Just use the .dt accessor and .days attribute on the timedeltas.

df.days_from = df.days_from.dt.days
miradulo
  • 28,857
  • 6
  • 80
  • 93