使用.iat时出现Python错误

使用.iat时出现Python错误,python,python-3.x,pandas,Python,Python 3.x,Pandas,当创建具有两列相同列名的数据框时,使用.iat[i,j]将导致TypeError。 但是,切换到.iloc[i,j]将解决问题。 为什么在这种情况下,iat的行为与iloc不同 python版本:3.6.1 熊猫版本:0.20.1 import pandas as pd x = pd.DataFrame([[1,2,3],[4,5,6]],columns=['a','b','a']) x.iloc[1,1] # works fine x.iat[1,1] # TypeError TypeEr

当创建具有两列相同列名的数据框时,使用
.iat[i,j]
将导致TypeError。 但是,切换到
.iloc[i,j]
将解决问题。 为什么在这种情况下,
iat
的行为与
iloc
不同

python版本:3.6.1 熊猫版本:0.20.1

import pandas as pd
x = pd.DataFrame([[1,2,3],[4,5,6]],columns=['a','b','a'])
x.iloc[1,1] # works fine
x.iat[1,1]  # TypeError
TypeError:未调整大小的对象的len()


当列名不唯一时(在这种情况下),您可能会遇到充当索引器的函数:

def _iget_item_cache(self, item):
    """Return the cached item, item represents a positional indexer."""
    ax = self._info_axis
    if ax.is_unique:
        lower = self._get_item_cache(ax[item])
    else:
        lower = self._take(item, axis=self._info_axis_number,
                           convert=True)
    return lower
由于
ax.is\u unique
为False,因此会调用
self.\u take
,问题是此函数调用
可能会转换索引
,它需要一个数组,但只得到一个
int
,程序崩溃,因为
mask=index<0
是bool,并且没有
any()
方法

两种解决方案:
好的:
避免使用相同的命名列,您的程序可以使用
x=pd.DataFrame([[1,2,3],[4,5,6]],列=['a','b','c'])正常运行。

丑陋的人: 修改pands源并更改
lower=self.\u take(项目,axis=self.\u info\u axis\u编号,convert=True)
具有
lower=self.\u take([item],axis=self.\u info\u axis\u number,convert=True)

附言:
如果将
x.at[1,'b']
与同名列一起使用,则会出现相同的问题。

似乎当列名不唯一时(在这种情况下),您会遇到此充当索引器的函数:

def _iget_item_cache(self, item):
    """Return the cached item, item represents a positional indexer."""
    ax = self._info_axis
    if ax.is_unique:
        lower = self._get_item_cache(ax[item])
    else:
        lower = self._take(item, axis=self._info_axis_number,
                           convert=True)
    return lower
由于
ax.is\u unique
为False,因此会调用
self.\u take
,问题是此函数调用
可能会转换索引
,它需要一个数组,但只得到一个
int
,程序崩溃,因为
mask=index<0
是bool,并且没有
any()
方法

两种解决方案:
好的:
避免使用相同的命名列,您的程序可以使用
x=pd.DataFrame([[1,2,3],[4,5,6]],列=['a','b','c'])正常运行。

丑陋的人: 修改pands源并更改
lower=self.\u take(项目,axis=self.\u info\u axis\u编号,convert=True)
具有
lower=self.\u take([item],axis=self.\u info\u axis\u number,convert=True)

附言: 如果将
x.at[1,'b']
与列同名,则会出现相同的问题