繁体   English   中英

TypeError: 'DataFrame' 对象是可变的,因此它们不能被散列 - 在创建数据帧字典时

[英]TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed- when creating dictionary of dataframes

我正在尝试创建从yahoo导入的数据框字典。

这不起作用:

import pandas_datareader.data as web
symbols = ['BTC-EUR','ETH-EUR' ]
dict_of_stock = { web.DataReader(s, "yahoo") for s in (symbols) }

出去:

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in __hash__(self)
  1797     def __hash__(self):
  1798         raise TypeError(
-> 1799             f"{repr(type(self).__name__)} objects are mutable, "
  1800             f"thus they cannot be hashed"
  1801         )

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

但是,这样做:

import pandas_datareader.data as web
symbols = ['BTC-EUR','ETH-EUR' ]

dict_of_stock = {}

for s in symbols:
   #do some calcs to get a dataframe called 'df'
   dict_of_stock[s] = web.DataReader(s, "yahoo")

是什么原因?

更改此行:

dict_of_stock = { web.DataReader(s, "yahoo") for s in (symbols) }

至:

dict_of_stock = { s: web.DataReader(s, "yahoo") for s in (symbols) }

在您之前的方法中,字典的键丢失,因此 DataFrame 被假定为键。 这导致了错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM