繁体   English   中英

在分组 dataframe 中将 MultiIndex 转换为 DatetimeIndex

[英]Convert MultiIndex to DatetimeIndex in grouped dataframe

我有一个 pandas DataFrameGroupBy (df_groups),它是通过对另一个 dataframe (df_pub) 进行分组而创建的,其中包含按日/月/年索引列出的出版物列表。

df_groups = df_pub.groupby(by=df_pub.index.day, df_pub.index.month,df_pub.index.year],sort=False)

然后我想检查每个组中有多少独特的出版物,所以我使用:

n_unique_pub = df_groups.Title.nunique()

这是一个 pandas 系列,其 MutiIndex 如下所示:

MultiIndex([( 1,  7, 2020),
            ( 2,  7, 2020),
            ( 3,  7, 2020),
            ( 4,  7, 2020),
            ( 5,  7, 2020),
            ( 6,  7, 2020),
            ( 7,  7, 2020),
            ( 8,  7, 2020),
            ( 9,  7, 2020),
            (10,  7, 2020),
            ...
            ( 8, 11, 2021),
            ( 9, 11, 2021),
            (10, 11, 2021),
            (11, 11, 2021),
            (12, 11, 2021),
            (13, 11, 2021),
            (14, 11, 2021),
            (15, 11, 2021),
            (16, 11, 2021),
            (17, 11, 2021)],
           names=['Date', 'Date', 'Date'], length=497)

我想将此 MultiIndex 转换为 DatetimeIndex ,使其看起来像:

DatetimeIndex(['2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04',
               '2020-07-05', '2020-07-06', '2020-07-07', '2020-07-08',
               '2020-07-09', '2020-07-10',
               ...
               '2021-11-08', '2021-11-09', '2021-11-10', '2021-11-11',
               '2021-11-12', '2021-11-13', '2021-11-14', '2021-11-15',
               '2021-11-16', '2021-11-17'],
              dtype='datetime64[ns]', name='Date', length=505, freq='D')

有没有简单的方法来做到这一点? 到目前为止,我已经尝试了几种方法,但都没有奏效。 例如,如果我执行pd.to_datetime(n_unique_pub.index)我有一个错误: TypeError: <class 'tuple'> is not convertible to datetime

使用pd.to_datetime

# mi is your MultiIndex instance, like mi = df.index
>>> pd.DatetimeIndex(pd.to_datetime(mi.rename(['day', 'month', 'year']).to_frame()))

DatetimeIndex(['2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04',
               '2020-07-05', '2020-07-06', '2020-07-07', '2020-07-08',
               '2020-07-09', '2020-07-10', '2021-11-08', '2021-11-09',
               '2021-11-10', '2021-11-11', '2021-11-12', '2021-11-13',
               '2021-11-14', '2021-11-15', '2021-11-16', '2021-11-17'],
              dtype='datetime64[ns]', freq=None)

如何将 MultiIndex 替换为 DatetimeIndex:

idx = pd.to_datetime(df.index.rename(['day', 'month', 'year']).to_frame())
df = df.set_index(idx)
print(df)

# Output:
                   A
2020-07-01  0.961038
2020-07-02  0.098132
2020-07-03  0.406996
2020-07-04  0.008376
2020-07-05  0.568059
2020-07-06  0.576610
2020-07-07  0.137144
2020-07-08  0.672219
2020-07-09  0.142874
2020-07-10  0.509231
2021-11-08  0.368762
2021-11-09  0.249107
2021-11-10  0.136282
2021-11-11  0.119291
2021-11-12  0.052388
2021-11-13  0.434899
2021-11-14  0.770705
2021-11-15  0.850914
2021-11-16  0.621283
2021-11-17  0.379888

您可以先转换为 'YYYY-MM-DD' 格式:

idx = pd.MultiIndex.from_tuples(
    [( 1,  7, 2020),
     ( 2,  7, 2020),]
    )

pd.to_datetime(idx.map(lambda x: '-'.join(map(str, reversed(x)))))

Output:

DatetimeIndex(['2020-07-01', '2020-07-02'], dtype='datetime64[ns]', freq=None)

像应该工作的东西

dates = pd.to_datetime(df_groups.reset_index()[['year','month','day']]) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM