1

I am trying to find the day difference between today, and dates in my dataframe.

Below is my conversion of dates in my dataframe

df['Date']=pd.to_datetime(df['Date'])

Below is my code to get today

today1=dt.datetime.today().strftime('%Y-%m-%d')
today1=pd.to_datetime(today1)

Both are converted to pandas.to_datetime, but when I do subtraction, the below error came out.

ValueError: Cannot add integral value to Timestamp without offset.

Can someone help to advise? Thanks!

2
  • What line produces the error? How do you perform the subtraction? Commented Dec 14, 2016 at 3:29
  • works fine for me. check the types/dtypes to make sure you have what you think you have Commented Dec 14, 2016 at 3:39

1 Answer 1

1

This is a simple example how you can do this:

import pandas
import datetime as dt

First, you have to get today.

today1=dt.datetime.today().strftime('%Y-%m-%d')
today1=pd.to_datetime(today1)

Then, you can construct the data frame:

df = pandas.DataFrame({'Date':'2016-11-24 11:03:10.050000', 'today1': today1 }, index = [0])

In this example I just have 2 columns, each with one value.

Next, you should check the data types:

print(df.dtypes)
Date      datetime64[ns]
today1    datetime64[ns]

If both data types are datetime64[ns], you can then subtract df.Date from df.today1.

print(df.today1 - df.Date)

The output:

0   19 days 12:56:49.950000
dtype: timedelta64[ns]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.