pandas 有一个代码,我试图写取数据形式雅虎金融,但我面临这些问题

5gfr0r5j  于 2022-12-28  发布在  其他
关注(0)|答案(1)|浏览(179)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader as data 

start ='2010-01-01'
end = '2019-12-31'

df=data.DataReader('AppL', 'yahoo', start, end)
df.head()
  • ---错误-----
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_1028/3510576555.py in <module>
      2 end = '2019-12-31'
      3 
----> 4 df=data.DataReader('AppL', 'yahoo', start, end)
      5 df.head()

~/conda/envs/python/lib/python3.7/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    205                 else:
    206                     kwargs[new_arg_name] = new_arg_value
--> 207             return func(*args, **kwargs)
    208 
    209         return cast(F, wrapper)

~/conda/envs/python/lib/python3.7/site-packages/pandas_datareader/data.py in DataReader(name, data_source, start, end, retry_count, pause, session, api_key)
    376             retry_count=retry_count,
    377             pause=pause,
--> 378             session=session,
    379         ).read()
    380 

~/conda/envs/python/lib/python3.7/site-packages/pandas_datareader/base.py in read(self)
    251         # If a single symbol, (e.g., 'GOOG')
    252         if isinstance(self.symbols, (string_types, int)):
--> 253             df = self._read_one_data(self.url, params=self._get_params(self.symbols))
    254         # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
    255         elif isinstance(self.symbols, DataFrame):

~/conda/envs/python/lib/python3.7/site-packages/pandas_datareader/yahoo/daily.py in _read_one_data(self, url, params)
    151         try:
    152             j = json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))
--> 153             data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
    154         except KeyError:
    155             msg = "No data fetched for symbol {} using {}"

TypeError: string indices must be integers

请帮助我解决这段代码中的错误。

abithluo

abithluo1#

根据documentation,您可以使用以下代码在start_dateend_date之间从yahoo finance下载数据:
这将产生一个**Pandas Dataframe **。

代码:
import yfinance as yf
start_date ='2010-01-01' 
end_date = '2019-12-31'
data = yf.download("AAPL", start=start_date, end=end_date)
print(data)
输出:
Open       High        Low      Close  Adj Close     Volume
Date                                                                        
2010-01-04   7.622500   7.660714   7.585000   7.643214   6.515214  493729600
2010-01-05   7.664286   7.699643   7.616071   7.656429   6.526476  601904800
2010-01-06   7.656429   7.686786   7.526786   7.534643   6.422664  552160000
2010-01-07   7.562500   7.571429   7.466071   7.520714   6.410791  477131200
2010-01-08   7.510714   7.571429   7.466429   7.570714   6.453412  447610800
...               ...        ...        ...        ...        ...        ...
2019-12-23  70.132500  71.062500  70.092499  71.000000  69.557091   98572000
2019-12-24  71.172501  71.222504  70.730003  71.067497  69.623215   48478800
2019-12-26  71.205002  72.495003  71.175003  72.477501  71.004578   93121200
2019-12-27  72.779999  73.492500  72.029999  72.449997  70.977631  146266000
2019-12-30  72.364998  73.172501  71.305000  72.879997  71.398895  144114400

[2515 rows x 6 columns]

注:

如果您想要多个股票代码

import yfinance as yf
start_date ='2010-01-01' 
end_date = '2019-12-31'
data = yf.download("MSFT AAPL", start=start_date, end=end_date)
print(data)

相关问题