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]
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With