當我在 pandas 中使用 23.35 提取小時數時,dt.hour它回傳 0。但我想得到 24。怎么做?
uj5u.com熱心網友回復:
假設這個輸入:
s = pd.to_datetime(pd.Series(['00:10', '12:30', '23:35']))
0 2022-04-18 00:10:00
1 2022-04-18 12:30:00
2 2022-04-18 23:35:00
dtype: datetime64[ns]
小時
s.dt.hour
0 0
1 12
2 23
dtype: int64
確實,如果您round:
s.dt.round('h').dt.hour
0 0
1 12
2 0
dtype: int64
你可以做什么來轉換四舍五入的小時 分鐘
s.dt.hour.add(s.dt.minute/60).round().astype(int)
0 0
1 12
2 24
dtype: int64
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459832.html
