2

I'm appending a column to my pandas dataframe which is the time difference between two dates.

df['time_diff'] = datetime.dt(2018,1,1) - df['IN_TIME'] 

the type if the new column in <m8[ns]. I'm trying to filter the rows whose 'time_diff' is greater than 30 days but I can't compare <m8[ns] with a number. How can I do this comparison?

1
  • Take the datetime difference as an object and that object will have a days int property. Commented Jun 4, 2018 at 20:44

1 Answer 1

5

Here's one way. Note you don't need to use the datetime module for these calculations as Pandas has some intuitive functionality for these operations.

df['time_diff'] = pd.to_datetime('2018-01-01') - df['IN_TIME'] df = df[df['time_diff'].dt.days > 30] 

This solution assumes df['IN_TIME'] is a datetime series; if it is not, you can convert via df['IN_TIME'] = pd.to_datetime(df['IN_TIME']).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.