pandas中的asfreq返回空 Dataframe

nwwlzxa7  于 2023-06-20  发布在  其他
关注(0)|答案(2)|浏览(151)

我特灵使用inf_freq和asfreq,返回的数据框是空的
原始数据集:

month   interest
0   2004-01 13
1   2004-02 15
2   2004-03 17
3   2004-04 19
4   2004-05 22

尝试转换不同频率的数据

ice_cream_interest = pd.read_csv('ice_cream_interest.csv')
ice_cream_interest.set_index('month', inplace=True)
ice_cream_interest = ice_cream_interest.asfreq(pd.infer_freq(ice_cream_interest.index))
interest
month   
2004-01-01  NaN
2004-02-01  NaN
2004-03-01  NaN
2004-04-01  NaN
2004-05-01  NaN
... ...
2020-04-01  NaN
2020-05-01  NaN
2020-06-01  NaN
2020-07-01  NaN
2020-08-01  NaN
nzkunb0c

nzkunb0c1#

我想引用asfreq()的文档。
对应于新索引中不存在于原始索引中的任何时间步长的值将为空(NaN)。
问题是,在你的例子中,索引中的日期仍然是字符串,并且asfreq()-方法给定的datetime对象还不存在。您可以解决这个问题,首先将“months”转换为datetime对象。
请看下面的例子:

import pandas as pd
df = pd.DataFrame({
    'month': ['2004-01', '2004-02', '2004-03', '2004-04', '2004-05'],
    'interest': [13, 15,17, 19, 22]
})

如果我们首先将月份转换为datetime对象,我们将得到

df['month'] = pd.to_datetime(df['month'])
df = df.set_index('month')
print(df.index.values) # this is a list of datetime objects
>>> ['2004-01-01T00:00:00.000000000' '2004-02-01T00:00:00.000000000'
 '2004-03-01T00:00:00.000000000' '2004-04-01T00:00:00.000000000'
 '2004-05-01T00:00:00.000000000']
df = df.asfreq(pd.infer_freq(df.index))
df
>>>
            interest
month               
2004-01-01        13
2004-02-01        15
2004-03-01        17
2004-04-01        19
2004-05-01        22

在没有这种转换的情况下也做同样的事情

# df['month'] = pd.to_datetime(df['month'])
df = df.set_index('month')
print(df.index.values)  # this is still a list strings
>>> ['2004-01' '2004-02' '2004-03' '2004-04' '2004-05']

df = df.asfreq(pd.infer_freq(df.index))
df
>>>
            interest
month               
2004-01-01       NaN
2004-02-01       NaN
2004-03-01       NaN
2004-04-01       NaN
2004-05-01       NaN
cgvd09ve

cgvd09ve2#

给出:

month  interest
0  2004-01        13
1  2004-02        15
2  2004-03        17
3  2004-04        19
4  2004-05        22

正在进行:

# Convert to datetime
df.month = pd.to_datetime(df.month)

# Set Index
df = df.set_index('month')

# Convert to freq
df = df.asfreq(pd.infer_freq(df.index))

输出:

>>> df
            interest
month
2004-01-01        13
2004-02-01        15
2004-03-01        17
2004-04-01        19
2004-05-01        22

>>> df.index
DatetimeIndex(['2004-01-01', '2004-02-01', '2004-03-01', '2004-04-01',
               '2004-05-01'],
              dtype='datetime64[ns]', name='month', freq='MS')

我们可以看到它成功地转换为频率指数。

相关问题