我想洗掉熊貓 df 索引中日期值的年份和分鐘部分:
DatetimeIndex(['2022-05-16 14:31:14', '2022-05-16 16:31:15',
'2022-05-16 18:31:16', '2022-05-16 20:31:17',
'2022-05-16 22:31:18', '2022-05-17 00:31:19',
'2022-05-17 02:31:20', '2022-05-17 04:31:21',
'2022-05-17 06:31:22', '2022-05-17 08:31:23',
...
'2022-06-11 06:12:18', '2022-06-11 08:12:18',
'2022-06-11 10:12:18', '2022-06-11 12:12:18',
'2022-06-11 14:12:18', '2022-06-11 16:12:18',
'2022-06-11 18:12:18', '2022-06-11 20:12:18',
'2022-06-11 22:12:18', '2022-06-12 00:12:18'],
dtype='datetime64[ns]', name=0, length=320, freq=None)
我希望整個陣列中的值的格式是'05-16 14',(月,日,小時)
如何做到這一點?
uj5u.com熱心網友回復:
如果您想保留 datetime 型別,這是不可能的,您必須使用以下方法轉換為字串strftime
:
df.index.strftime('%m-%d %H')
輸出:
Index(['05-16 14', '05-16 16', '05-16 18', '05-16 20', '05-16 22', '05-17 00',
'05-17 02', '05-17 04', '05-17 06', '05-17 08', '06-11 06', '06-11 08',
'06-11 10', '06-11 12', '06-11 14', '06-11 16', '06-11 18', '06-11 20',
'06-11 22', '06-12 00'],
dtype='object')
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/496330.html
標籤:Python 熊猫 约会时间 numpy-ndarray 日期时间格式