英文:
TypeError: string indices must be integers ,What should I do
问题
以下是您要求的代码的中文翻译部分:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader as data
start = "2010-01-01"
end = "2020-12-30"
df = data.DataReader('AAPL', 'yahoo', start, end)
df.head()
TypeError Traceback (most recent call last)
/tmp/ipykernel_13005/124255995.py in <module>
2 end = "2020-12-30"
3
----> 4 df = data.DataReader(data['AAPL'],'yahoo', start, end)
5 df.head()
TypeError: 'module' object is subscriptable
英文:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader as data
start = "2010-01-01"
end = "2020-12-30"
df = data.DataReader('AAPL','yahoo', start, end)
df.head()
TypeError Traceback (most recent call last)
/tmp/ipykernel_13005/124255995.py in <module>
2 end = "2020-12-30"
3
----> 4 df = data.DataReader(data['AAPL'],'yahoo', start, end)
5 df.head()
TypeError: 'module' object is not subscriptable
This is what I Expected! ,can anyone help to solve this.Kindly
答案1
得分: 1
Most likely its an import error.
According to data reader docs this is how you should import data_reader:
import pandas_datareader.data as web
df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')
instead you have:
import pandas_datareader as data
The first example is importing data
from pandas_datareader
module and calling DataReader()
that is inside data
.
However, in your example you are trying to call the DataReader()
method of pandas_datareader
module (which does not exist because this method is inside pandas_datareader.data
)
Hence the error:
TypeError: 'module' object is not subscriptable
英文:
Most likely its an import error.
According to data reader docs this is how you should import data_reader:
import pandas_datareader.data as web
df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')
instead you have:
import pandas_datareader as data
The first example is importing data
from pandas_datareader
module and calling DataReader()
that is inside data
.
However, in your example you are trying to call the DataReader()
method of pandas_datareader
module (which does not exist because this method is inside pandas_datareader.data
)
Hence the error:
TypeError: 'module' object is not subscriptable
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论