【问题标题】:Resample time-series and show time of day重新采样时间序列并显示一天中的时间
【发布时间】:2018-06-05 18:50:36
【问题描述】:

这快把我逼疯了。我试图找到时间序列的最大每日值,同时显示它发生的时间。我有一个日期时间索引、第二个日期时间列和一个最高温度。如果我这样做了

temperature_max = temperature.resample('D').max()

我得到了时间 (23:59) 和温度的最大值。如何获得最高温度发生的时间?非常感谢!

【问题讨论】:

    标签: python python-3.x pandas datetime python-datetime


    【解决方案1】:

    我相信您需要idxmax 来获取最大temperature 的索引,然后通过loc 选择:

    temperature_max = temperature.loc[temperature.resample('D')['temperature'].idxmax()]
    

    示例:

    rng = pd.date_range('2017-04-03', periods=10)
    r = pd.date_range('2017-04-03', periods=10, freq='12H')
    temperature = pd.DataFrame({'Date': rng, 'temperature': range(10)}, index=r)  
    print (temperature)
                              Date  temperature
    2017-04-03 00:00:00 2017-04-03            0
    2017-04-03 12:00:00 2017-04-04            1
    2017-04-04 00:00:00 2017-04-05            2
    2017-04-04 12:00:00 2017-04-06            3
    2017-04-05 00:00:00 2017-04-07            4
    2017-04-05 12:00:00 2017-04-08            5
    2017-04-06 00:00:00 2017-04-09            6
    2017-04-06 12:00:00 2017-04-10            7
    2017-04-07 00:00:00 2017-04-11            8
    2017-04-07 12:00:00 2017-04-12            9
    
    temperature_max = temperature.loc[temperature.resample('D')['temperature'].idxmax()]
    print (temperature_max)
                              Date  temperature
    2017-04-03 12:00:00 2017-04-04            1
    2017-04-04 12:00:00 2017-04-06            3
    2017-04-05 12:00:00 2017-04-08            5
    2017-04-06 12:00:00 2017-04-10            7
    2017-04-07 12:00:00 2017-04-12            9
    

    【讨论】:

    • 这正是我想要的。谢谢!
    猜你喜欢
    • 2014-08-09
    • 2020-08-15
    • 2012-11-26
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2013-06-02
    相关资源
    最近更新 更多