【问题标题】:How to set the start time and end time in calculating moving averages?如何设置计算移动平均线的开始时间和结束时间?
【发布时间】:2019-07-09 08:55:11
【问题描述】:

我正在尝试根据 10 秒的记录数据计算 15 分钟、8 小时和 24 小时的移动平均值。如何设置计算移动平均线的开始时间和结束时间?

我使用以下代码计算移动平均线

 Gas_432.set_index('RecTime').rolling(90).mean()
 Gas_432.between_time('2019-05-31 00:00:00','2019-07-04 08:08:21')

但我收到一条错误消息 TypeError: Index must be DatetimeIndex

【问题讨论】:

    标签: python pandas time-series moving-average


    【解决方案1】:

    创建DatetimeIndex后使用DataFrame.truncate

    #if not datetimes
    Gas_432['RecTime'] = pd.to_datetime(Gas_432['RecTime'])
    
    Gas_432 = (Gas_432.set_index('RecTime')
                      .truncate('2019-05-31 00:00:00','2019-07-04 08:08:21')
                      .rolling(90)
                      .mean())
    

    【讨论】:

    • 我收到一个错误ValueError: Cannot convert arg ['2019-05-31 00:00:00'] to a time
    • @D.Banerjee - Gas_432['RecTime'] = pd.to_datetime(Gas_432['RecTime'], errors='coerce') 工作怎么样?
    • 第一行代码运行良好。我收到第二行代码的错误
    • @D.Banerjee - 看来你的熊猫版本太旧了,所以尝试将两个字符串都转换为日期时间.between_time(pd.to_datetime('2019-05-31 00:00:00'),pd.to_datetime('2019-07-04 08:08:21'))
    • 收到错误ValueError: Cannot convert arg [Timestamp('2019-05-31 00:00:00')] to a time
    猜你喜欢
    • 2011-03-27
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    相关资源
    最近更新 更多