gpt4 book ai didi

pandas - 如何使用 pd.to_datetime() 设置频率?

转载 作者:行者123 更新时间:2023-12-04 00:28:09 25 4
gpt4 key购买 nike

拟合统计模型时,我收到有关日期频率的警告。

首先,我导入一个数据集:

import statsmodels as sm
df = sm.datasets.get_rdataset(package='datasets', dataname='airquality').data

df['Year'] = 1973
df['Date'] = pd.to_datetime(df[['Year', 'Month', 'Day']])

df.drop(columns=['Year', 'Month', 'Day'], inplace=True)
df.set_index('Date', inplace=True, drop=True)

接下来我尝试拟合 SES 模型:
fit = sm.tsa.api.SimpleExpSmoothing(df['Wind']).fit()

返回此警告:

/anaconda3/lib/python3.6/site-packages/statsmodels/tsa/base/tsa_model.py:171: ValueWarning: No frequency information was provided, so inferred frequency D will be used. % freq, ValueWarning)



我的数据集是每天,所以推断“D”是可以的,但我想知道如何手动设置频率。

请注意, DatetimeIndex 没有频率(最后一行)......
DatetimeIndex(['1973-05-01', '1973-05-02', '1973-05-03', '1973-05-04',
'1973-05-05', '1973-05-06', '1973-05-07', '1973-05-08',
'1973-05-09', '1973-05-10',
...
'1973-09-21', '1973-09-22', '1973-09-23', '1973-09-24',
'1973-09-25', '1973-09-26', '1973-09-27', '1973-09-28',
'1973-09-29', '1973-09-30'],
dtype='datetime64[ns]', name='Date', length=153, freq=None)

根据 this answer我检查了缺少的日期,但似乎没有:
pd.date_range(start = '1973-05-01', end = '1973-09-30').difference(df.index)

DatetimeIndex([], dtype='datetime64[ns]', freq='D')



我应该如何设置索引的频率?

最佳答案

我想pd.to_datetime未设置默认频率,需要 DataFrame.asfreq :

df = df.set_index('Date').asfreq('d')
print (df.index)

DatetimeIndex(['1973-05-01', '1973-05-02', '1973-05-03', '1973-05-04',
'1973-05-05', '1973-05-06', '1973-05-07', '1973-05-08',
'1973-05-09', '1973-05-10',
...
'1973-09-21', '1973-09-22', '1973-09-23', '1973-09-24',
'1973-09-25', '1973-09-26', '1973-09-27', '1973-09-28',
'1973-09-29', '1973-09-30'],
dtype='datetime64[ns]', name='Date', length=153, freq='D')

但是如果索引中的重复值出现错误:
df = pd.concat([df, df])
df = df.set_index('Date')

print (df.asfreq('d').index)

ValueError: cannot reindex from a duplicate axis



解决方法是使用 resample带有一些聚合函数:
print (df.resample('2D').mean().index)

DatetimeIndex(['1973-05-01', '1973-05-03', '1973-05-05', '1973-05-07',
'1973-05-09', '1973-05-11', '1973-05-13', '1973-05-15',
'1973-05-17', '1973-05-19', '1973-05-21', '1973-05-23',
'1973-05-25', '1973-05-27', '1973-05-29', '1973-05-31',
'1973-06-02', '1973-06-04', '1973-06-06', '1973-06-08',
'1973-06-10', '1973-06-12', '1973-06-14', '1973-06-16',
'1973-06-18', '1973-06-20', '1973-06-22', '1973-06-24',
'1973-06-26', '1973-06-28', '1973-06-30', '1973-07-02',
'1973-07-04', '1973-07-06', '1973-07-08', '1973-07-10',
'1973-07-12', '1973-07-14', '1973-07-16', '1973-07-18',
'1973-07-20', '1973-07-22', '1973-07-24', '1973-07-26',
'1973-07-28', '1973-07-30', '1973-08-01', '1973-08-03',
'1973-08-05', '1973-08-07', '1973-08-09', '1973-08-11',
'1973-08-13', '1973-08-15', '1973-08-17', '1973-08-19',
'1973-08-21', '1973-08-23', '1973-08-25', '1973-08-27',
'1973-08-29', '1973-08-31', '1973-09-02', '1973-09-04',
'1973-09-06', '1973-09-08', '1973-09-10', '1973-09-12',
'1973-09-14', '1973-09-16', '1973-09-18', '1973-09-20',
'1973-09-22', '1973-09-24', '1973-09-26', '1973-09-28',
'1973-09-30'],
dtype='datetime64[ns]', name='Date', freq='2D')

关于pandas - 如何使用 pd.to_datetime() 设置频率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54630027/

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