I have two columns. I want to check if the difference between them is between 0 and 10 days. One of the fields often contains null values.
df['Diff'] = (df['Dt1'] - df['Dt2'])
def wdw(x):
if pd.notnull(x):
if type(x) !=long:
if type(timedelta(days=10)) != long:
if x > timedelta(days=10):
return 1
else:
return 0
df['Diff'].df(wdw)
When I run this I get the following error.
TypeError: can't compare datetime.timedelta to long
When I look at the values of df['Diff'] They all appear to be timedeltas. Any idea what is going on here? It seems like creating an indicator based on the difference between two date fields should be easier than this...
The values in
df['Diff']
are numpy timedelta64s. You can compare them withpd.Timedelta
s; see below.Moreover, you do not need to call
df['Diff'].apply(wdw)
, which callswdw
for each value in the Series; you can compare whole Series with apd.Timedelta
:yields
pd.Timedelta
s were introduced in Pandas v.0.15. Here is a workaround for older version of Pandas which usesnp.timedela64s
: