Search code examples
datetimenumpypandasdate-arithmetic

Detect consecutive dates in pandas series of DatetimeIndex


I have pandas Series of DatetimeIndex in date format (YYYY-MM-DD) and want to label consecutive regions, where each index is consecutive in respect to a day - so if there is a missing date in a Datetime series, I want to detect it, i.e.:

...
2005-01-15
2005-01-16
2005-01-17
2005-02-15
2005-02-16
...

where a gap of missing days between 2005-01-17 and 2005-02-15 is evident.

Couldn't find easy way to do this with pandas, while I expect some helper function that I'm not aware of. More generally, also numpy solution would be appreciated.


@smci, I don't know what dput() is, but here is one way to generate sample data:

import pandas as pd
import numpy as np

data = pd.concat([
    pd.Series(np.random.randn(3), pd.date_range('2005-01-15', '2005-01-17')),
    pd.Series(np.random.randn(3), pd.date_range('2005-02-15', '2005-02-17'))
])

Solution

  • Try something like:

    data.index - data.index.shift(1, freq=pd.DateOffset(1))
    

    per @chrisb's answer to Calculating time difference between two rows