我有一段功能代码,但是,我遇到了内存错误或运行时间过长,是否有更优雅的解决方案需要更少的内存或者可以在更短的时间内运行?
.product-items__item
答案 0 :(得分:1)
import pandas.io.data
:
In [113]: import pandas.io.data
...
ImportError: The pandas.io.data module is moved to a separate package (pandas-datareader). After installing the pandas-datareader package (h
ttps://github.com/pandas-dev/pandas-datareader), you can change the import ``from pandas.io import data, wb`` to ``from pandas_datareader im
port data, wb``.
所以我们应该使用pandas_datareader
代替:
from pandas_datareader import data as web
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
sp500 = pd.read_html(url)[0].iloc[1:, 0].str.replace('\.', '-')
df = web.DataReader(sp500, "yahoo", '2000-01-01').to_frame()
内存使用情况:
In [112]: df.memory_usage()
Out[112]:
Index 7974167
Open 15870936
High 15870936
Low 15870936
Close 15870936
Volume 15870936
Adj Close 15870936
dtype: int64
执行时间:
In [115]: %timeit -n 1 -r 1 web.DataReader(sp500, "yahoo", '2000-01-01').to_frame()
1 loop, best of 1: 1min 57s per loop