gpt4 book ai didi

python - Pandas DatetimeIndex 奇怪的行为

转载 作者:行者123 更新时间:2023-12-01 03:40:05 26 4
gpt4 key购买 nike

我处理一个DataFrame,其索引是字符串,年月,例如:

index = ['2007-01', '2007-03', ...]

但是,索引未满。例如缺少 2007-02。我想要的是使用完整索引重新索引 DataFrame。

我尝试过的:

In [60]: pd.DatetimeIndex(start='2007-01', end='2007-12', freq='M')
Out[60]:
DatetimeIndex(['2007-01-31', '2007-02-28', '2007-03-31', '2007-04-30',
'2007-05-31', '2007-06-30', '2007-07-31', '2007-08-31',
'2007-09-30', '2007-10-31', '2007-11-30'],
dtype='datetime64[ns]', freq='M')

索引是每个月的月底。

In [64]: pd.DatetimeIndex(['2007-01', '2007-03', '2007-04', '2007-05'])
Out[64]: DatetimeIndex(['2007-01-01', '2007-03-01', '2007-04-01', '2007-05-01'], dtype='datetime64[ns]', freq=None)

索引是每个月的开始。

如何处理这个问题?

最佳答案

如果需要每月第一天的频率,我认为您需要添加参数freq='MS':

print (pd.DatetimeIndex(start='2007-01', end='2007-12', freq='MS'))
DatetimeIndex(['2007-01-01', '2007-02-01', '2007-03-01', '2007-04-01',
'2007-05-01', '2007-06-01', '2007-07-01', '2007-08-01',
'2007-09-01', '2007-10-01', '2007-11-01', '2007-12-01'],
dtype='datetime64[ns]', freq='MS')

链接到Offset Aliases in pandas documentation ,谢谢EdChum .

另一个解决方案是使用 PeriodIndex用于生成月份期间:

print (pd.PeriodIndex(start='2007-01', end='2007-12', freq='M'))
PeriodIndex(['2007-01', '2007-02', '2007-03', '2007-04', '2007-05', '2007-06',
'2007-07', '2007-08', '2007-09', '2007-10', '2007-11', '2007-12'],
dtype='int64', freq='M')

关于python - Pandas DatetimeIndex 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39720641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com