我正在使用一个小时数据框,该数据框包含从 2016 年开始一天中每个小时的行。我想从这个小时数据框中删除我在“df_outlayers”数据框中的一些天数,这是一个每日数据框。我尝试了以下方法:
remove = df_hourly.loc[df_outlayers.index]
df_clean = df_hourly.drop(remove.index)
df_clean['2017-04-17']
但它只是删除了一天中的第一个小时,例如它删除了行 2017-04-17 00:00:00 而不是 2017-04-17 01:00:00。对于那些给定的 outlayers 天数,我如何每小时删除一次?
注意:我的“df_outlayers”数据框有一个名为日期的索引列,df_outlayers.index 给出了例如:
DatetimeIndex(['2016-07-06', '2016-07-08', '2016-10-10', '2017-04-09',
'2017-04-17', '2017-04-26', '2017-07-05', '2017-07-07',
'2017-09-01', '2017-09-22', '2017-09-29'],
dtype='datetime64[ns]', name='date', freq=None)
我的 df_hourly 数据框也有一个名为“date”的索引列,df_hourly.index 给出了例如:
DatetimeIndex(['2014-07-19 00:00:00', '2014-07-19 01:00:00', ...]
dtype='datetime64[ns]', name='date', length=13214, freq=None)
请您参考如下方法:
看来你需要boolean indexing通过 ~ 和 numpy.in1d 反转掩码,因为 DatetimeIndex.date返回 numpy 数组:
mask = np.in1d(df_hourly.index.date, df_outlayers.index.date)
df_clean = df_hourly[~mask]
