16

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?

1
  • have you tried df['Date'].date()?
    – Yuca
    Commented Sep 11, 2018 at 14:42

4 Answers 4

16

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.

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.

1
  • 2
    Performance comment, this question from today seems to demonstrate that dt.days doesn't actually take advantage of the underlying NumPy arrays.
    – jpp
    Commented Sep 11, 2018 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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.