14

I am trying to subtract today's date from a column in pandas to get the number of days(as an integer).

I first converted the date's in column(ex: 27-Sep-2018) using pd.to_datetime.

df['Date'] - datetime.datetime.now().date()

I got the following error:

TypeError: unsupported operand type(s) for -: 'DatetimeIndex' and 'datetime.date'

I am trying to figure out how to get this to work, also converting the days to integer?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sid
  • 3,749
  • 7
  • 29
  • 62

4 Answers4

13

I think the issue may be due to you subtracting a pandas datetime object from a date object (which does not include the time). You can try this:

df['Date_2'] = pd.to_datetime(df['Date']).dt.date

Now doing the calculation: df['Date_2'] - datetime.datetime.now().date() should work.

SAKURA
  • 937
  • 11
  • 29
4

Let's use pandas Timestamp.now():

s = pd.Series('27-Sep-2018')

s = pd.to_datetime(s)

(s - pd.Timestamp.now()).dt.days

Output:

0     15
dtype: int64

Note: The error is stating that you can't subtract object type DatetimeIndex from object 'datetime.date'. So, use pandas Timestamp to create the same object type as DateTimeIndex.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • 2
    Performance comment, [this question from today](https://stackoverflow.com/questions/52274356/conversion-of-a-timedelta-to-int-very-slow-in-python/52274596#52274596) seems to demonstrate that `dt.days` doesn't actually *take advantage* of the underlying NumPy arrays. – jpp Sep 11 '18 at 14:56
2

try to use datetime.strptime() function to convert it.

in your ex='27-Sep-2018' it would look like these:

from datetime import datetime
ex='27-Sep-2018'
date = datetime.strptime(ex, '%d-%b-%Y')

and then:

date.days 

will store result (type - int)

2
index[0].to_pydatetime()

See here: https://www.geeksforgeeks.org/python-pandas-datetimeindex-to_pydatetime/

Mike Reiche
  • 382
  • 3
  • 12