pandas DataReader 工作不正常

pandas DataReader is not working correctly

我正在尝试从 yahoo finance 导入数据,但 pandas 似乎没有正确读取开始日期和结束日期。 还报告我 pandas 我不明白

的错误

这是我输入的代码:

import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt

这是屏幕上显示的内容,但我仍然可以使用 pandas

/opt/anaconda3/lib/python3.7/site-packages/pandas_datareader/compat/__init__.py:7: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  from pandas.util.testing import assert_frame_equal.

然后我运行这个代码

acciones=["PG","BEI.DE"]
datos= pd.DataFrame()
for t in acciones:
    datos[t]=wb.DataReader(t,data_source="yahoo",start=2016-1-1,end=2019-1-1)["Adj Close"]

当我检查输出日期延迟两年时,我不知道为什么

datos.tail()

Date        PG             BEI.DE
2016-12-23  76.435783   78.406380
2016-12-27  76.111885   78.726517
2016-12-28  75.635086   78.600410
2016-12-29  75.886978   78.687721
2016-12-30  75.644073   78.192947


datos.head
Date        PG             BEI.DE
2014-01-02  65.854416   68.331200
2014-01-03  65.780823   68.686317
2014-01-06  65.936180   68.405960
2014-01-07  66.573967   68.592857
2014-01-08  65.609123   68.004128

您收到警告 FutureWarning: pandas.util.testing is deprecated 因此您仍然可以 运行 您的代码,但它可能会在将来中断。此问题已解决 here

不要使用导入语句:from pandas.util.testing import assert_frame_equal请改用这个

from pandas.testing import assert_frame_equal

您还应该使用日期时间库来创建开始日期和结束日期,以便您的日期类型正确。

import datetime
import pandas as pd
import pandas_datareader.data as wb

start_date = datetime.datetime(2016,1,1)
end_date = datetime.datetime(2019,1,1)
acciones=["PG","BEI.DE"]
datos= pd.DataFrame()
for t in acciones:
    datos[t]=wb.DataReader(t,data_source="yahoo",start=start_date,end=end_date)["Adj Close"]

输出:

>>> datos.head()
                   PG     BEI.DE
Date                            
2016-01-04  68.264992  78.003090
2016-01-05  68.482758  78.849281
2016-01-06  67.820770  78.339645
2016-01-07  67.228455  76.426102
2016-01-08  66.174454  76.233788

>>> datos.tail()
                   PG     BEI.DE
Date                            
2018-12-24  83.908928        NaN
2018-12-26  86.531075        NaN
2018-12-27  88.384834  89.214600
2018-12-28  87.578018  89.805695
2018-12-31  88.288788        NaN