重新采样时间序列并显示时间

时间:2017-12-23 13:40:50

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

这让我发疯了。我试图找到时间序列的最大每日价值,同时显示它发生的时间。我有datetimeindex,第二个datetime列和最高温度。如果我做

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

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

1 个答案:

答案 0 :(得分:2)

我认为您需要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