我目前有點堅持獲取我的資料集的每日最大值。它看起來像這樣:
Date Value
0 1996-03-07 21:30:00 360.0
1 1996-03-07 21:45:00 360.0
2 1996-03-07 22:00:00 360.0
3 1996-03-07 22:15:00 360.0
4 1996-03-07 22:30:00 360.0
... ... ...
867882 2021-02-03 12:45:00 361.9
867883 2021-02-03 13:00:00 361.8
867884 2021-02-03 13:15:00 361.8
867885 2021-02-03 13:30:00 361.7
867886 2021-02-03 13:45:00 361.8
[867887 rows x 2 columns]
問題是在資料集中缺少一整天。如果我理解正確,熊貓中的石斑魚需要持續幾天才能正常作業。所以我重新填寫了日期:
df.set_index('Date', inplace=True)
all_days = pd.date_range(df.index.min(), df.index.max(), freq='15T')
df = df.reindex(all_days)
但是當我現在運行我的代碼以獲得每日最大值時
daily_maximum = df.loc[df.groupby(pd.Grouper(freq='D')).idxmax().iloc[:, 0]]
我收到以下錯誤訊息:
The DTypes <class 'numpy.dtype[float64]'> and <class 'numpy.dtype[datetime64]'> do not have a common DType. For example they cannot be stored in a single array unless the dtype is `object`.
當我檢查時df.index,我得到
DatetimeIndex(['1996-03-07 21:30:00', '1996-03-07 21:45:00',
'1996-03-07 22:00:00', '1996-03-07 22:15:00',
'1996-03-07 22:30:00', '1996-03-07 22:45:00',
'1996-03-07 23:00:00', '1996-03-07 23:15:00',
'1996-03-07 23:30:00', '1996-03-07 23:45:00',
...
'2021-02-03 11:30:00', '2021-02-03 11:45:00',
'2021-02-03 12:00:00', '2021-02-03 12:15:00',
'2021-02-03 12:30:00', '2021-02-03 12:45:00',
'2021-02-03 13:00:00', '2021-02-03 13:15:00',
'2021-02-03 13:30:00', '2021-02-03 13:45:00'],
dtype='datetime64[ns]', length=873474, freq='15T')
并檢查我的Value列回傳的 dtype dtype('float64')。
我可能在這里遺漏了一些非常明顯的東西,但老實說,我對 dtypes 和日期格式一點也不熟悉。
uj5u.com熱心網友回復:
我認為這里的問題是沒有定義列之后groupby,所以沒有回傳Series,而是DataFrame:
daily_maximum = df.loc[df.groupby(pd.Grouper(freq='D'))['value'].idxmax()]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/435932.html
