Skip to content

Series.__getitem__ semantics vary according to runtime values #9213

Open
@dandavison

Description

Depending on the runtime value used in [ indexing on a series, the semantics may be array-indexing, or slicing based on a MultiIndex. This is extremely error-prone behavior in a very basic operation.

Is the answer that production code should always use .loc/.ix instead of [? If so, I don't think the docs make that clear.

>>> s = pandas.Series({(1, 1): 11, (1, 2): 12, (2, 1): 21, (2, 2): 22})

>>> s

1  1    11
   2    12
2  1    21
   2    22
dtype: int64

#0 isn't recognized as something to do with the index
# so treat it as an array index
>>> s[0]  
    11

# But this could be asking for a slice,
# so return a slice rather than an array-indexed element.
>>> s[1] 

1    11
2    12
dtype: int64

>>> pandas.__version__
    '0.15.2'

See also #3390

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions