API: getitem[int] vs setitem[int] with Float64Index · Issue #33469 · pandas-dev/pandas (original) (raw)
ser = pd.Series([1, 2, 3, 4], index=[1.1, 2.1, 3.0, 4.1])
>>> ser[5] # <--we are treating 5 as a label
KeyError: 5
>>> ser[5] = 5 # < --we are treating 5 as position
IndexError: index 5 is out of bounds for axis 0 with size 4
>>> ser[3] = 5 # <-- we are treating 3 as a label
>>> ser
1.1 1
2.1 2
3.0 5
4.1 4
dtype: int64
The ser[5] = 5
case is an outlier because instead of having its label-vs-positional behavior determined by ser.index._should_fallback_to_positional
, it is defermined by if is_integer(key) and not self.index.inferred_type == "integer":