TypeError: 字符串索引必须是整数,应该怎么办?

huangapple go评论82阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月26日 23:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定