简体   繁体   English

来自熊猫数据框的多索引的日期索引

[英]date index from from multiindex for pandas dataframe

I have a dataframe with multiindex which I want to convert to date() index.我有一个带有 multiindex 的数据框,我想将其转换为date()索引。

Here is an example emulation of the type of dataframes I have:这是我拥有的数据帧类型的示例仿真:

i = pd.date_range('01-01-2016', '01-01-2020')
x = pd.DataFrame(index = i, data=np.random.randint(0, 10, len(i)))
x = x.groupby(by = [x.index.year, x.index.month]).sum()
print(x)

I tried to convert it to date index by this:我尝试通过以下方式将其转换为日期索引:

def to_date(ind):
    return pd.to_datetime(str(ind[0]) + '/' + str(ind[1]), format="%Y/%m").date()

# flattening the multiindex to tuples to later reset the index
x.set_axis(x.index.to_flat_index(), axis=0, inplace = True)    

x = x.rename(index = to_date)

x.set_axis(pd.DatetimeIndex(x.index), axis=0, inplace=True)

But it is very slow.但它很慢。 I think the problem is in the pd.to_datetime(str(ind[0]) + '/' + str(ind[1]), format="%Y/%m").date() line.我认为问题出在pd.to_datetime(str(ind[0]) + '/' + str(ind[1]), format="%Y/%m").date()行中。 Would greatly appreciate any ideas to make this faster.非常感谢任何使这更快的想法。

You can just use:你可以只使用:

x.index=pd.to_datetime([f"{a}-{b}" for a,b in x.index],format='%Y-%m')
print(x)

            0
2016-01-01  162
2016-02-01  119
2016-03-01  148
2016-04-01  125
2016-05-01  132
2016-06-01  144
2016-07-01  157
2016-08-01  141
2016-09-01  138
2016-10-01  168
2016-11-01  140
2016-12-01  137
2017-01-01  113
2017-02-01  113
2017-03-01  155
..........
..........
......

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

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