Merge DataFrames based on conditioning datetime64

251 views Asked by At

I am trying to merge 2 Dfs under curtain conditioning on dates.

df 1: enter image description here

df2: enter image description here

Let's say to today is 2015-01-12, what I'm trying to do is for every clienthostid that has first_activity date earlier than 10 days before today to be excluded from df2, so in the example here I'd be left if df2 with: enter image description here

I tried to do so by first merging the 2 df:

temp = pd.merge(df1, df2, on='clienthostid', how='inner')

and then try to delete according to the condition:

temp = temp[temp.First_activity + 10 < today]

and I'm getting this error:

TypeError: cannot operate on a series without a rhs of a series/ndarray of type datetime64[ns] or a timedelta

First_activity and today are datetime64...

I'm not familiar much with sql, python nor pandas (with sums it all I guess :)), but I've got an assignment to implement this so sorry if the question stupid.

1

There are 1 answers

0
jezrael On BEST ANSWER

I think you need convert to_timedelta int value or use offsets:

today = pd.datetime.today().date() 
temp = temp[temp.First_activity < today - pd.to_timedelta(10, unit='d')]

Or:

temp = temp[temp.First_activity < today - pd.offsets.Day(10)]