为什么当在系列中设置或获取具有错误索引数量的项目时,pandas的行为会有所不同:
df = pd.DataFrame({'a': [10]})
# df['a'] is a series, can be indexed with 1 index only
# will raise IndexingError, as expected
df['a'].iloc[0, 0]
df['a'].loc[0, 0]
# will raise nothing, not as expected
df['a'].iloc[0, 0] = 1000 # equivalent to pass
df['a'].loc[0, 0] = 1000 # equivalent to df['a'].loc[0] = 1000
# pandas version 0.18.1, python 3.5
编辑:已报告。
TypeError
,而不是IndexingError
,所以我错了。我不知道Pandas为什么允许你做第二个。 - John GordonSeries
,因为df['a']
是一个Series
(你会得到相同的行为)。 - Alex