2017-01-10 88 views
2

我想在日期窗簾條件下合併2個Dfs。基於條件合併DataFrames datetime64

DF 1: enter image description here

DF2: enter image description here

比方說,今天是2015-01-12,我想要做的是對具有first_activity日期早於10每clienthostid今天天前從DF2被排除,所以在這裏的例子我會留下,如果DF2有: enter image description here

我試着這樣做:首先合併2 DF:

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

,然後嘗試根據條件刪除:

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

,我得到這個錯誤:

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

First_activity今天是datetime64 ...

我並不熟悉sql,python和pandas(總結一切,我猜:)), 但我有一個任務來實現這個很抱歉如果問題愚蠢。

回答

1

我想你需要轉換to_timedelta int值或使用offsets

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

或者:

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