python - 乘以长度为 1 的熊猫系列时出现键错误“0”
问题描述
由于某些原因,我目前正在运行一个 for 循环,其中A x N
数组(实际上是xarray
数据数组,但问题仍然存在numpy
于下面的 MRE 中的基本数组)乘以N x 1
pandas 系列(形成A x 1
数组),用于各种N
s。
只要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
.
先感谢您!
解决方案
推荐阅读
- android - 这里 Maps MapView 在 Fragment 中不起作用
- graphite - 石墨网和 ubuntu 18.04
- r - 在 R 中运行 voronoi tessellation 后,图块数小于点数
- shell - 使用 aws cli 命令在 AWS Cognito 中操作回调 URL
- android - Cordova config.xml xmlns:android 命名空间在每次构建中都会被删除
- android - 使用 WCF 服务改进 POST 方法得到错误代码 400:错误请求
- c - 使用来自 GCC 的 4 位 PowerPC CR0 寄存器
- sql-server - 每月工作日
- r - 从 R 中的列表中提取 SMD
- twig - Twig:判断一个菜单项是否有翻译