从每分钟的熊猫中删除几秒钟

时间:2020-04-22 16:36:54

标签: python pandas algorithm time-series

我想知道如何从较大的时间单位中删除一小段时间。让我们说我们有一天的数据集,我们想从这一天开始删除每分钟的前10秒。如何在Pandas或Numpy中做到这一点?

该示例显示了15分钟范围内的值,并且删除了上午06点至上午10点之间的值。这应该每天在数据集中发生。我希望你能帮助我。

Before:
2019-01-01 05:15:00    0.0
2019-01-01 05:30:00    0.0
2019-01-01 05:45:00    0.0
2019-01-01 06:00:00    0.0
2019-01-01 06:15:00    0.0

After:
2019-01-01 05:15:00    0.0
2019-01-01 05:30:00    0.0
2019-01-01 05:45:00    0.0
2019-01-01 10:15:00    0.0
2019-01-01 10:30:00    0.0

谢谢。

编辑:

我尝试了这个,并且奏效了:

#The actual deleting of the rows between 6am and 10 am
def delete_row_by_time(df, day):
  from_ts = day + ' 06:00:00'
  to_ts = day +  ' 10:00:00'
  df = df[(df.index < from_ts) | (df.index > to_ts)]
  return df

#Get the actual days
days = eins.index.strftime('%Y-%m-%d').unique()
days = pd.to_datetime(days)

start_date = days.min()
end_date = days.max()
delta = datetime.timedelta(days=1)

#iterate through all days in dataset
while start_date <= end_date:
  print(start_date)
  df = delete_row_by_time(df, str(start_date))
  start_date += delta

也许有一些改进。

1 个答案:

答案 0 :(得分:0)

以前的解决方案无法正常工作,因为您没有DateTime列,但是没有DateTimeIndex,因此语法略有不同。

您的解决方案有效,但是,可以使用矢量化的pandas函数解决此问题,因此您不必每天{@ {1}}循环运行

for/while

此解决方案将在上午6点至上午10点之间(包括10:00:00)每小时删除一次