本文介绍了Pandas:如何将系列的 MultiIndex 折叠为 DateTimeIndex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 Pandas groupby:按学期分组的后续,我需要折叠一个系列的 MultiIndex 到 DateTimeIndex.

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

我已经看过将 Pandas 多索引折叠为单索引 但无济于事.我无法让它发挥作用.

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

系列 ser 是:

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

如何将 dtime 折叠为单个 DateTimeIndex?

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

这是生成我的演示系列 ser 的代码:

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)

推荐答案

您需要将 MultiIndexSeries 的级别连接在一起并转换为 datetimes>:

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

或者:

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

这篇关于Pandas:如何将系列的 MultiIndex 折叠为 DateTimeIndex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 18:17