在创建DataFrame之后设置 pandas DatetimeIndex的频率 [英] Setting freq of pandas DatetimeIndex after DataFrame creation

查看:347
本文介绍了在创建DataFrame之后设置 pandas DatetimeIndex的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pandas datareader来获取库存数据.

Im using pandas datareader to get stock data.

import pandas as pd
import pandas_datareader.data as web
ABB = web.DataReader(name='ABB.ST', 
                     data_source='yahoo',
                     start='2000-1-1')

但是默认情况下,未在结果数据帧上设置freq. 我需要freq才能使用这样的索引进行导航:

However by default freq is not set on the resulting dataframe. I need freq to be able to navigate using the index like this:

for index, row in ABB.iterrows():
    ABB.loc[[index + 1]]

如果未在DatetimeIndex上设置频率,则无法使用+1等进行导航.

If freq is not set on DatetimeIndex im not able to use +1 etc to navigate.

我发现有两个功能astyperesample.由于我已经知道频率resample看起来有些过激,所以我只想将频率设置为每天.

What I have found are two functions astype and resample. Since I already know to freq resample looks like overkill, I just want to set freq to daily.

现在我的问题是如何在ABB上使用astype将频率设置为每日?

Now my question is how can i use astype on ABB to set freq to daily?

推荐答案

尝试:

ABB = ABB.asfreq('d')

这应将频率更改为每天使用NaN连续几天无数据.

This should change the frequency to daily with NaN for days without data.

此外,您还应该按如下方式重写for-loop:

Also, you should rewrite your for-loop as follows:

for index, row in ABB.iterrows():
    print(ABB.loc[[index + pd.Timedelta(days = 1)]])

谢谢!

这篇关于在创建DataFrame之后设置 pandas DatetimeIndex的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆