【问题标题】:duplicates by timestamp difference pandas not working按时间戳差异重复熊猫不起作用
【发布时间】:2018-10-10 17:06:52
【问题描述】:

我找到了this example。我需要在一秒钟内获取下一行具有相同价格的所有行。所以这与另一个问题相同,但对我不起作用。

我使用的是 python 3.6.3 和 pandas 版本 0.22.0

我的 df:

                    timestamp   price
    0  2018-04-29 13:14:43.026  6394.0
    1  2018-04-29 13:16:53.714  6378.0
    2  2018-04-29 13:17:01.324  6378.0
    3  2018-04-29 13:17:02.246  6384.0
    4  2018-04-29 13:17:07.413  6384.0
    5  2018-04-29 13:17:08.331  6380.0
    6  2018-04-29 13:17:11.437  6380.0
    7  2018-04-29 13:17:11.895  6377.0
    8  2018-04-29 13:17:13.449  6377.0
    9  2018-04-29 13:17:13.452  6379.0

    timestamp    datetime64[ns]
    price               float64
    dtype: object

我尝试以下代码:

 data = df[(df.groupby(["price"], as_index=False)["timestamp"].diff().fillna(0).dt.seconds <= 1).reset_index(drop=True)]

结果如下:

                    timestamp   price
    0 2018-04-29 13:14:43.026  6394.0
    1 2018-04-29 13:16:53.714  6378.0
    2 2018-04-29 13:17:01.324  6378.0
    4 2018-04-29 13:17:07.413  6384.0
    5 2018-04-29 13:17:08.331  6380.0
    7 2018-04-29 13:17:11.895  6377.0
    9 2018-04-29 13:17:13.452  6379.0

编辑:

输出应该是空的,但如果我们制作这个数据框,即

                    timestamp   price
    0  2018-04-29 13:14:43.026  6394.0
    1  2018-04-29 13:16:53.714  6378.0
    2  2018-04-29 13:17:01.324  6378.0
    3  2018-04-29 13:17:02.246  6378.0
    4  2018-04-29 13:17:07.413  6384.0
    5  2018-04-29 13:17:08.331  6380.0
    6  2018-04-29 13:17:11.437  6380.0
    7  2018-04-29 13:17:11.895  6377.0
    8  2018-04-29 13:17:13.449  6377.0
    9  2018-04-29 13:17:13.452  6377.0

它应该输出:

                    timestamp   price

    2  2018-04-29 13:17:01.324  6378.0
    3  2018-04-29 13:17:02.246  6378.0
    8  2018-04-29 13:17:13.449  6377.0
    9  2018-04-29 13:17:13.452  6377.0

【问题讨论】:

  • 问题在于您的.fillna(0)。你用 0 填充,这意味着每一个都出现在你的 &lt; 0 的逻辑中。填充其他值,例如 999
  • 你的预期输出是什么?
  • @ALollz 它应该只用于第一个值,因为它们都有时间戳。所以应该不重要。
  • @user3605780 由于您首先进行分组,然后执行.diff(),因此您获得的NaN 值比您认为的要多得多。在每个组中,差异的第一行是 NaN,而不仅仅是 10 行的第一行 df
  • @cᴏʟᴅsᴘᴇᴇᴅ 我添加了额外的示例数据

标签: python pandas


【解决方案1】:

除非我弄错了,否则我认为你有点过于复杂了,你应该只需要比较一下:

df = pd.read_fwf(StringIO(
'''timestamp                price
2018-04-29 13:14:43.026  6394.0
2018-04-29 13:16:53.714  6378.0
2018-04-29 13:17:01.324  6378.0
2018-04-29 13:17:02.246  6378.0
2018-04-29 13:17:07.413  6384.0
2018-04-29 13:17:08.331  6380.0
2018-04-29 13:17:11.437  6380.0
2018-04-29 13:17:11.895  6377.0
2018-04-29 13:17:13.449  6377.0
2018-04-29 13:17:13.452  6379.0'''
), colspecs=[(0,23), (25, 31)], dtype={
    'timestamp': 'datetime64[ns]',
    'price': 'float'
})


diff = df.diff()
selection = (diff['timestamp'].dt.seconds <=1) & (diff['price'] == 0)
selection = selection | selection.shift(periods=-1)
print(df[selection])

输出

                timestamp   price
2 2018-04-29 13:17:01.324  6378.0
3 2018-04-29 13:17:02.246  6378.0
7 2018-04-29 13:17:11.895  6377.0
8 2018-04-29 13:17:13.449  6377.0

【讨论】:

  • @user3605780 抱歉,这里停顿了一秒,我是想换个方向,现在应该有正确的输出了。
猜你喜欢
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2015-01-09
  • 1970-01-01
相关资源
最近更新 更多