【问题标题】:Get the time span of a time series in pandas获取熊猫时间序列的时间跨度
【发布时间】:2019-04-30 14:41:46
【问题描述】:

我正在尝试获取每组条目(时间序列)的时间跨度。条目没有排序。在 pandas 中是否有比使用 max-min 更有效的方法?我已经尝试了 timedelta 和 period 函数,但它们没有返回我需要的东西。

import pandas as pd
tt = pd.DataFrame([
[1,'2018-3-1' ],
[1,'2018-2-3' ],
[1,'2018-5-2' ],
[1,'2018-4-5' ],
[2,'2017-3-10' ],
[2,'2017-1-12' ],
[2,'2017-5-15' ],
[2,'2017-2-14' ]
],columns=['group','entry'])

tt.entry = pd.to_datetime(tt.entry)

tt.groupby('group')['entry'].apply(lambda x: max(x)-min(x))

# group
# 1    88 days
# 2   123 days
# Name: entry, dtype: timedelta64[ns]

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    这是ptp 来自numpy 的一种方式

    tt.groupby('group')['entry'].apply(np.ptp)
    Out[773]: 
    group
    1    88 days
    2   123 days
    Name: entry, dtype: timedelta64[ns]
    

    【讨论】:

    • 我收到DatetimeIndex cannot perform the operation ptp
    • @RBA 怎么样tt.groupby('group')['entry'].apply(pd.Series.ptp)
    • 遇到了与tt.groupby('group')['entry'].apply(pd.Series.ptp) Pandas 0.24.2 相同的错误
    • @RBA 我相信这是 pandas 的问题,我会在 git 上提交
    • 谢谢!您可以在哪个 pandas 版本中运行 np.ptp?我将在一个单独的环境中降级,看看它是否能提高我的性能
    猜你喜欢
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2014-10-08
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多