Example: I have intraday dataframe (no freqency in days)
df = Column
Date
2005-01-03 08:00:00 88.6008
2005-01-03 08:01:00 88.5571
2005-01-03 08:02:00 88.5571
2005-01-03 08:03:00 88.5717
2005-01-03 08:04:00 88.5644
... ...
2021-10-08 19:54:00 437.3900
2021-10-08 19:56:00 437.3900
2021-10-08 19:58:00 437.3700
2021-10-08 19:59:00 437.3500
2021-10-08 20:00:00 437.8600
And i need to do something with all days except some special days like
special days = Index(['2007-07-27', '2007-08-10', '2008-08-18', '2008-09-10', '2008-09-19',
'2008-09-22', '2008-09-29', '2008-10-01', '2008-10-14', '2008-10-15',
'2008-11-20', '2008-11-21', '2008-11-24', '2008-12-01', '2008-12-02',
'2008-12-15', '2008-12-22', '2009-01-09', '2009-01-15', '2009-01-20',
'2009-02-18', '2009-03-30', '2009-04-17', '2009-05-12', '2009-07-02',
'2009-11-19', '2010-01-28', '2010-05-04', '2010-05-14', '2010-08-18',
'2010-09-28', '2011-03-10', '2011-08-09', '2011-08-10', '2011-09-09',
'2011-09-13', '2011-10-05', '2011-10-07', '2011-10-25', '2012-11-07',
'2015-01-12', '2015-01-22', '2015-04-01', '2015-08-25', '2015-08-26',
'2016-01-14', '2018-02-09', '2020-03-03', '2020-03-10', '2020-03-13',
'2020-03-17', '2020-03-19', '2020-03-23', '2020-04-07', '2020-04-13',
'2020-05-04', '2020-06-29', '2020-09-21', '2020-11-06', '2020-11-09',
'2021-01-27', '2021-01-29'],
dtype='object')
Well, only now noticed that i didnt make them DatetimeIndex by to_datetime(), nevermind, I'll make it.
So... I can make something like this: tmp_df = df.loc[:special_days[0]]
Do something to that part, then do something to that part: tmp_df[special_days[0]]
And finally, I need next day of special_days[0]
but to be sure that next day will be in dataframe data and loc from the next day till special_days[1] and do whatever I want.
Well, i know, there is some kind of easy solution like split my original data in parts something like this:
df_normal_days = df[~df.index.isin(special_days)]
df_special_days = df[df.index.isin(special_days)]
and iterate over pairs of dates, but is there any way to get next day? Like get_loc or something :/
CodePudding user response:
Use:
special_days = pd.to_datetime(special_days)
idx_norm = df.index.normalize()
for days in zip(special_days, special_days[1:]):
print (days)
df_special_days = df[idx_norm.isin(days)]