首页 > 解决方案 > 乘以长度为 1 的熊猫系列时出现键错误“0”

问题描述

由于某些原因,我目前正在运行一个 for 循环,其中A x N数组(实际上是xarray数据数组,但问题仍然存在numpy于下面的 MRE 中的基本数组)乘以N x 1pandas 系列(形成A x 1数组),用于各种Ns。

只要N>1. 但是一旦N=1(也就是整个数组乘以相同的值),KeyError就会出现一个奇数(见下文)。

import pandas as pd
import numpy as np

# Size 2x1 series, multiplication works fine
a = np.reshape(np.array(np.random.rand(10,2)),(10,-1))
b = [pd.Series(data=[1.0,1.0],index=[1,2])]
a*b


# Size 1x1 series, multiplication fails 
a = np.reshape(np.array(np.random.rand(10,1)),(10,-1))
b = [pd.Series(data=[1.0],index=[1])]
a*b
# With following error message
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-112-d29b22165258> in <module>
      1 a = np.reshape(np.array(np.random.rand(10,1)),(10,-1))
      2 b = [pd.Series(data=[1.0],index=[1])]
----> 3 a*b

~/opt/anaconda3/envs/climate1/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~/opt/anaconda3/envs/climate1/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4402         k = self._convert_scalar_indexer(k, kind="getitem")
   4403         try:
-> 4404             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4405         except KeyError as e1:
   4406             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

我找到了一种解决方法,可以用浮点数明确替换系列:

# This works fine
a = np.reshape(np.array(np.random.rand(10,1)),(10,-1))
b = [pd.Series(data=[1.0],index=[1])]
a*[float(k) for k in b]

但我希望能帮助我了解此错误消息的来源;因为我怀疑我误解了关于pandas.

先感谢您!

标签: pythonarrayspandasdataframenumpy

解决方案


推荐阅读