数据帧的频率 [英] Frequency of a data frame
问题描述
我有一个用日期索引的数据框(Python datetime对象).我怎么能找到频率作为数据帧中数据的月数?
I have a data frame indexed with a date (Python datetime object). How could I find the frequency as the number of months of data in the data frame?
我尝试了属性data_frame.index.freq
,但是它返回了none值.我还尝试了使用data_frame.asfreq('M',how={'start','end'}
的asfreq
函数,但是它没有返回预期的结果.请告知我如何获得预期的结果.
I tried the attribute data_frame.index.freq
, but it returns a none value. I also tried asfreq
function using data_frame.asfreq('M',how={'start','end'}
but it does not return the expected results. Please advise how I can get the expected results.
推荐答案
您要将日期时间的索引转换为DatetimeIndex,最简单的方法是使用
You want to convert you index of datetimes to a DatetimeIndex, the easiest way is to use to_datetime
:
df.index = pd.to_datetime(df.index)
现在您可以执行时间序列/框架操作,例如重新采样或TimeGrouper.
Now you can do timeseries/frame operations, like resample or TimeGrouper.
如果您的数据频率一致,则为df.index.freq
,如果没有(例如,如果缺少某几天),则df.index.freq
为None.
If your data has a consistent frequency, then this will be df.index.freq
, if it doesn't (e.g. if some days are missing) then df.index.freq
will be None.
这篇关于数据帧的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!