0

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'))
])
smci
  • 32,567
  • 20
  • 113
  • 146
theta
  • 24,593
  • 37
  • 119
  • 159
  • Thanks for adding the example. Doh! `dput()` is from R, not pandas, my brain thunked the wrong direction. – smci Dec 28 '14 at 19:47
  • 1
    Near-dupe of: [Calculating time difference between two rows](http://stackoverflow.com/questions/25328125/calculating-time-difference-between-two-rows/), [Describing gaps in a time series pandas](http://stackoverflow.com/questions/24815720/describing-gaps-in-a-time-series-pandas) and [pandas TimeSeries diff() reverts to Series](http://stackoverflow.com/questions/24597446/pandas-timeseries-diff-reverts-to-series) – smci Dec 28 '14 at 19:56

2 Answers2

1

Try something like:

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

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

Community
  • 1
  • 1
smci
  • 32,567
  • 20
  • 113
  • 146
0

Smci's answer did not work for detecting missing date as the question was asking.

I use DataFrame.asfreq('D') to detect missing values. Those missing dates will be listed but their corresponding values will show NAN. For example:

df1 = df.asfreq('D)
missing_dates=df1[df1.Column.isnull()]
Sarah
  • 1,854
  • 17
  • 18