pandas 整数的多索引到日期时间索引? [英] pandas multi-index of integers to datetime index?
问题描述
我有一个如下所示的数据框,其中包含代表一年中的月份和日期的多整数索引,以及这些天的最高和最低温度记录.
df最低温度 最高温度日期 日期1 1 -88 1392 -115 1503 -110 1394 -81 1565 -80 172…………12 2 -94 1563 -97 1724 -120 1565 -124 1446 -161 1307 -167 1358 -141 1679 -135 17810 -106 19411 -106 16112 -94 14413 -92 13314 -149 11715 -158 11716 -119 12217 -111 16018 -142 13319 -185 13020 -190 16121 -167 16122 -98 15023 -162 13924 -90 18325 -125 18326 -119 14427 -76 13028 -81 13429 -117 11330 -127 10631 -111 122
如何将此多索引转换为日期时间类型的单个索引?我正在寻找类似这种转换的东西:
1 1 --->1月1日1 2 --->1月2日...12 31 --->12 月 31 日
以数据框的顶部为例:
<预><代码>>>>df最低温度 最高温度日期 日期1 1 -88 1392 -115 1503 -110 1394 -81 1565 -80 172在 MultiIndex 的各个级别使用 pd.to_datetime
,然后使用您想要的格式strftime
:
df.index = pd.to_datetime(df.index.get_level_values(0).astype(str) + '-' +df.index.get_level_values(1).astype(str),format='%m-%d').strftime('%B %d')>>>df最低温度 最高温度1 月 1 日 -88 1391 月 2 日 -115 1501 月 3 日 -110 139一月 4 -81 1561月5日-80日172
但是,因为这是格式化的字符串,所以将不再是日期时间格式.如果您希望它是日期时间,则需要包括一年.您可以省略 strftime
,它将使用默认的 1900
:
df.index = pd.to_datetime(df.index.get_level_values(0).astype(str) + '-' +df.index.get_level_values(1).astype(str),格式='%m-%d')>>>df最低温度 最高温度1900-01-01 -88 1391900-01-02 -115 1501900-01-03 -110 1391900-01-04 -81 1561900-01-05 -80 172
I have a dataframe like the following, with a multi-index of integers that represents months and days of the year, along with maximum and minimum temperature recordings from those days.
df
Min Temp Max Temp
Date Date
1 1 -88 139
2 -115 150
3 -110 139
4 -81 156
5 -80 172
... ... ...
12 2 -94 156
3 -97 172
4 -120 156
5 -124 144
6 -161 130
7 -167 135
8 -141 167
9 -135 178
10 -106 194
11 -106 161
12 -94 144
13 -92 133
14 -149 117
15 -158 117
16 -119 122
17 -111 160
18 -142 133
19 -185 130
20 -190 161
21 -167 161
22 -98 150
23 -162 139
24 -90 183
25 -125 183
26 -119 144
27 -76 130
28 -81 134
29 -117 113
30 -127 106
31 -111 122
How can I convert this multi-index to a single index that is of type datetime? Something like this conversion is what I am looking for:
1 1 ---> January 1
1 2 ---> January 2
...
12 31 ---> December 31
Using the top of your dataframe as an example:
>>> df
Min Temp Max Temp
Date Date
1 1 -88 139
2 -115 150
3 -110 139
4 -81 156
5 -80 172
Use pd.to_datetime
on the individual levels of your MultiIndex, then strftime
with your desired format:
df.index = pd.to_datetime(df.index.get_level_values(0).astype(str) + '-' +
df.index.get_level_values(1).astype(str),
format='%m-%d').strftime('%B %d')
>>> df
Min Temp Max Temp
January 01 -88 139
January 02 -115 150
January 03 -110 139
January 04 -81 156
January 05 -80 172
However, because this is a formatted string, it will no longer be datetime format. If you want it to be datetime, you need to include a year. You can omit the strftime
and it will use the default 1900
:
df.index = pd.to_datetime(df.index.get_level_values(0).astype(str) + '-' +
df.index.get_level_values(1).astype(str),
format='%m-%d')
>>> df
Min Temp Max Temp
1900-01-01 -88 139
1900-01-02 -115 150
1900-01-03 -110 139
1900-01-04 -81 156
1900-01-05 -80 172
这篇关于pandas 整数的多索引到日期时间索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!