pandas 将np datetime64列转换为panda DatetimeIndex,并正确设置频率属性

bzzcjhmw  于 2023-01-04  发布在  其他
关注(0)|答案(1)|浏览(187)

复制我的数据:

import numpy as np
import pandas as pd
dts = ['2016-01-01', '2016-02-01', '2016-03-01', '2016-04-01',
               '2016-05-01', '2016-06-01', '2016-07-01', '2016-08-01',
               '2016-09-01', '2016-10-01', '2016-11-01', '2016-12-01',
               '2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01']

my_df = pd.DataFrame({'col1': range(len(dts)), 'month_beginning': dts})#, dtype={'month_beginning': np.datetime64})
my_df['month_beginning'] = my_df.month_beginning.astype(np.datetime64)

我需要将month_beginning设置为日期时间索引,特别是我需要将frequency属性正确设置为monthly
以下是我迄今为止尝试过的方法,以及它们是如何不起作用的:

初次尝试

my_df = my_df.set_index('month_beginning')

...然而,在执行上述操作之后,my_df.index显示一个日期时间索引,但带有freq=None

第二次尝试

dt_idx = pd.DatetimeIndex(my_df.month_beginning, freq='M')

......但这会引发以下错误:

ValueError: Inferred frequency MS from passed values does not conform to passed frequency M

...这是特别令人困惑的我,因为可以检查我的数据以上,我的dts/month-beginning系列实际上是每月,并没有错过任何月份...

pvabu6sv

pvabu6sv1#

可以使用asfreq将时间序列转换为指定的频率:

import pandas as pd

dts = ['2016-01-01', '2016-02-01', '2016-03-01', '2016-04-01',
       '2016-05-01', '2016-06-01', '2016-07-01', '2016-08-01',
       '2016-09-01', '2016-10-01', '2016-11-01', '2016-12-01',
       '2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01']

df = pd.DataFrame({'col1': range(len(dts)), 'month_beginning': dts})
df['month_beginning'] = pd.to_datetime(df['month_beginning'])

df.index = df['month_beginning'] 
df = df.asfreq("MS")

df.index
DatetimeIndex(['2016-01-01', '2016-02-01', '2016-03-01', '2016-04-01',
               '2016-05-01', '2016-06-01', '2016-07-01', '2016-08-01',
               '2016-09-01', '2016-10-01', '2016-11-01', '2016-12-01',
               '2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01'],
              dtype='datetime64[ns]', name='month_beginning', freq='MS')

相关问题