Why does .loc work with integer index positions?

风格不统一 提交于 2019-12-11 19:12:59

问题


The pandas documentation of .loc clearly states:

.loc is strictly label based, will raise KeyError when the items are not found, allowed inputs are:

A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index. This use is not an integer position along the index)

Contrary to that, this surprisingly works for pd.Series, not for pd.DataFrame:

import numpy as np
a = np.array([1,3,1,2])
import pandas as pd
s = pd.Series(a, index=["a", "b", "c", "d"])
s.loc["a"] # yields 1
s.loc[0] # should be strictly label-based, but it works and also yields 1

Do you know why?

来源:https://stackoverflow.com/questions/21574882/why-does-loc-work-with-integer-index-positions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!