Pandas: how to collapse a Series' MultiIndex to a DateTimeIndex?

走远了吗. 提交于 2021-01-28 01:22:15

问题


As a followup of Pandas groupby: group by semester I need to collapse a Series' MultiIndex to a DateTimeIndex.

I already gave a look at Collapse Pandas MultiIndex to Single Index but at no avail. I cannot make it work.

Series ser is:

dtime  dtime
2016   1        78.0
       7        79.0
2017   1        73.0
       7        79.0
2018   1        79.0
       7        71.0
Name: values, dtype: float64

How to collapse dtime to a single DateTimeIndex?

dtime
2016-01-01      78.0
2016-07-01      79.0
2017-01-01      73.0
2017-07-01      79.0
2018-01-01      79.0
2018-07-01      71.0
Name: values, dtype: float64

This is the code producing my demo Series ser:

from datetime import *
import pandas as pd
import numpy as np

np.random.seed(seed=1111)
days = pd.date_range(start="2016-02-15", 
                     end="2018-09-12",
                    freq="2W")

df = pd.DataFrame({"dtime":days, "values":np.random.randint(50, high=80, size=len(days))}).set_index("dtime")

# group by semester
year = df.index.year.astype(int)
month = (df.index.month.astype(int) - 1) // 6 * 6 + 1
grouped = df.groupby([year, month])

ser = grouped.describe()[("values", "max")].rename("values")
print(ser)

回答1:


You need join levels of MultiIndex or Series together and convert to datetimes:

idx = ser.index.get_level_values(0).astype(str) +  ser.index.get_level_values(1).astype(str)

ser.index = pd.to_datetime(idx, format='%Y%m')
print(ser)
2016-01-01    78.0
2016-07-01    79.0
2017-01-01    73.0
2017-07-01    79.0
2018-01-01    79.0
2018-07-01    71.0
Name: values, dtype: float64

Or:

dates = pd.to_datetime(year.astype(str) + month.astype(str), format='%Y%m')
grouped = df.groupby(dates)

ser = grouped.describe()[("values", "max")].rename("values")
print (ser)
2016-01-01    78.0
2016-07-01    79.0
2017-01-01    73.0
2017-07-01    79.0
2018-01-01    79.0
2018-07-01    71.0
Name: values, dtype: float64


来源:https://stackoverflow.com/questions/51857052/pandas-how-to-collapse-a-series-multiindex-to-a-datetimeindex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!