让我们用非恒定时间步长生成10行时间序列:
import pandas as pd
import numpy as np
x = pd.DataFrame(np.random.random(10),pd.date_range('1/1/2011', periods=5, freq='1min') \
.union(pd.date_range('1/2/2011', periods=5, freq='1min')))数据示例:
2011-01-01 00:00:00 0.144852
2011-01-01 00:01:00 0.510248
2011-01-01 00:02:00 0.911903
2011-01-01 00:03:00 0.392504
2011-01-01 00:04:00 0.054307
2011-01-02 00:00:00 0.918862
2011-01-02 00:01:00 0.988054
2011-01-02 00:02:00 0.780668
2011-01-02 00:03:00 0.831947
2011-01-02 00:04:00 0.707357现在,让我们将r定义为所谓的“返回”(连续行之间的差异):
r = x[1:] - x[:-1].values如何通过移除时差不为1分钟的r[i] 来清除数据?( r中正好有一个这样的行要清理)
发布于 2016-02-29 23:17:06
我想你需要以下几点:
In [26]:
x[(x.index.to_series().diff() == pd.Timedelta(1, 'm')) | (x.index.to_series().diff().isnull())]
Out[26]:
0
2011-01-01 00:00:00 0.367675
2011-01-01 00:01:00 0.128325
2011-01-01 00:02:00 0.772191
2011-01-01 00:03:00 0.638847
2011-01-01 00:04:00 0.476668
2011-01-02 00:01:00 0.992888
2011-01-02 00:02:00 0.944810
2011-01-02 00:03:00 0.171831
2011-01-02 00:04:00 0.316064这将使用to_series将索引转换为一个系列,因此我们可以调用diff,然后将其与时间增量为1分钟的时间进行比较,我们还将处理diff将返回NaT的第一行情况。
https://stackoverflow.com/questions/35711588
复制相似问题