Skip to main content
 首页 » 编程设计

python之使用 pandas 读取在线数据源时是否可以指定数据频率

2025年05月04日13xiaohuochai

使用 pandas.io.data.DataReader类,我可以直接从在线数据源读取 FRED :

import datetime 
import pandas.io.data 
 
t1 = datetime.datetime(1960, 1, 1) 
t2 = datetime.datetime(2014, 6, 1) 
 
gdp = pandas.io.data.DataReader("GDPC96", "fred", t1, t2) 
 
print(gdp.tail()) 

这会正确获取数据:

               GDPC96 
DATE                  
2013-04-01  15606.591 
2013-07-01  15779.938 
2013-10-01  15916.208 
2014-01-01  15831.652 
2014-04-01  16010.405 

但是,这些数据实际上没有任何与之关联的频率,因此 gdp.index.freq 只是 None。出于某种原因,我无法使用 gdp.asfreq("Q") 设置频率,可能是因为日期是每个季度第一个月的第一天,而不是最后一天的最后一天月。如果我尝试这样做:

print(gdp.asfreq("Q").tail()) 

我明白了:

            GDPC96 
2013-03-31     NaN 
2013-06-30     NaN 
2013-09-30     NaN 
2013-12-31     NaN 
2014-03-31     NaN 

我唯一的选择是在下载后以某种方式重新映射日期,还是有办法使用正确的频率直接下载这些数据,例如通过指定日期范围 (t = pandas.date_range(t1, t2, freq="Q"))?

现在我正在使用这种方法:

import datetime 
import pandas.io.data 
import scipy 
 
t1 = datetime.datetime(1960, 1, 1) 
t2 = datetime.datetime(2014, 6, 1) 
 
t = pandas.date_range(t1, t2 + datetime.timedelta(days=32), freq="Q") 
 
gdp = pandas.io.data.DataReader("GDPC96", "fred", t1, t2) 
print(gdp.tail())     
 
gdp.index = t 
print(gdp.tail()) 

哪个正确执行映射:

               GDPC96 
DATE                  
2013-04-01  15606.591 
2013-07-01  15779.938 
2013-10-01  15916.208 
2014-01-01  15831.652 
2014-04-01  16010.405 
 
               GDPC96 
2013-06-30  15606.591 
2013-09-30  15779.938 
2013-12-31  15916.208 
2014-03-31  15831.652 
2014-06-30  16010.405 

但我觉得应该有一个标准的方法(也许就是这样),因为这可能是一个常见的操作。

更新:根据答案,我使用了这段代码:

import datetime 
import pandas.io.data 
import pandas.tseries.offsets 
 
t1 = datetime.datetime(1960, 1, 1) 
t2 = datetime.datetime(2014, 6, 1) 
 
gdp = pandas.io.data.DataReader("GDPC96", "fred", t1, t2) 
gdp.index += pandas.tseries.offsets.QuarterEnd() 

当我在 Spyder 中(或从命令行)运行它时出现错误:

Traceback (most recent call last): 
 
  File "<ipython-input-1-0ac9a29f7f6a>", line 1, in <module> 
    runfile('C:\folder\plots.py', wdir='C:\folder') 
 
  File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile 
    execfile(filename, namespace) 
 
  File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 48, in execfile 
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace) 
 
  File "C:/folder/plots", line 10, in <module> 
    gdp.index += pandas.tseries.offsets.QuarterEnd() 
 
  File "C:\Anaconda3\lib\site-packages\pandas\core\index.py", line 950, in __add__ 
    return Index(self.view(np.ndarray) + other) 
 
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('O') 

请您参考如下方法:

我认为您的方法已经很不错了。在 Pandas 版本 0.15 或 较新的,你可以改用

import pandas.tseries.offsets as offsets 
gdp.index += offsets.QuarterEnd() 

结果

In [75]: gdp.index 
Out[75]:  
<class 'pandas.tseries.index.DatetimeIndex'> 
[1960-03-31, ..., 2014-06-30] 
Length: 218, Freq: Q-DEC, Timezone: None 

在 0.14 版本中,您可以使用

gdp.index = pd.DatetimeIndex(gdp.index) + offsets.QuarterEnd() 

相反。