jf328 jf328 - 1 year ago 155
Python Question

pandas elementwise difference between two DatetimeIndex

Is there a pandas idiomatic way to find the difference in days between two pandas DatetimeIndex?

>>> d1 = pd.to_datetime(['2000-01-01', '2000-01-02'])
>>> d2 = pd.to_datetime(['2001-01-01', '2001-01-02'])

operator is set difference, ie dates in d1 but not in d2.

>>> d1-d2
DatetimeIndex(['2000-01-01', '2000-01-02'], dtype='datetime64[ns]', freq=None)

IMO, this is not consistent with numpy and pure python behaviour. Not even pandas itself

>>> d2[0]-d1[0]
Timedelta('366 days 00:00:00')

This is what I want, but ugly.

>>> [d.days for d in d2.to_pydatetime() - d1.to_pydatetime()]
[366, 366]

Answer Source

You use np.subtract directly:

np.subtract(d2, d1)

Which'll give you a TimedeltaIndex as a result:

TimedeltaIndex(['366 days', '366 days'], dtype='timedelta64[ns]', freq=None)

Then if wanted use .days on that.

Another possible way:

pd.to_timedelta(d2.values - d1.values).days

Which'll leave you with:

array([366, 366])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download