Skip to content

DOC: Wrong generalization about slice indexing #57277

Closed
@memeplex

Description

@memeplex

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/pandas-docs/version/2.0.2/user_guide/indexing.html#slicing-ranges

Documentation problem

With Series, the syntax works exactly as with an ndarray, returning a slice of the values and the corresponding labels

And then shows examples of slice indexing df[i:j] where i and j are integers, and all of them behave as df.iloc[i:j] (that is, "exactly as with an ndarray").

But there is an exception (which I can't make sense of and fortunately is deprecated):

> pd.Series([1, 2, 3], [3., 2., 1.])[:3]

3.0    1
dtype: int64

So, when the index is float, integer slices behave loc-like. Please document this exception in a callout because it's dangerous to assume that the iloc-like behavior is general.

BTW, could someone give a rationale for this behavior? Is there a general rule that ends up in this weird situation, like "if index and slice are int -> iloc, if not then if slice is of the type of the index -> loc else if slice is int -> iloc else fail", and then an int slice is taken to be of the type of a float index?

Suggested fix for documentation

Document this exception in a callout because it's dangerous to assume that the iloc-like behavior is general.

Metadata

Metadata

Labels

DocsIndexingRelated to indexing on series/frames, not to indexes themselves

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions