Given a Pandas
series and a datetime
slice or range of dates, how can I get all dates from the datetime
slice or range that are in the series
?
Ex:
#my date time slice/ date_range
st = datetime.datetime(2014, 8, 31, 0, 0)
en = datetime.datetime(2014, 9, 7, 0, 0)
date_slice = slice(st,en)
rng = pd.date_range('08-31-2014', periods=8, freq='D')
#my series
s = pd.Series({'09-01-2014': 1,
'09-02-2014': 1,
'09-03-2014': np.nan,
'09-04-2014': 1,
'09-06-2014': 1})
In this example I would like to return an array of dates in str
or datetime
format
['08-31-2014', '09-05-2014' , '09-07-2014']
DatetimeIndex.difference returns a sorted set difference:
In [573]: (rng.difference(pd.to_datetime(s.index))).format()
Out[573]: ['2014-08-31', '2014-09-05', '2014-09-07']
In [598]: (rng.difference(pd.to_datetime(s.index))).format(formatter=lambda x: x.strftime('%m-%d-%Y'))
Out[598]: ['08-31-2014', '09-05-2014', '09-07-2014']