Skip to content

Series.drop() with MultiIndex: inconsistent behaviour #8594

Closed
@urraca

Description

@urraca

The behaviour below occurs in versions '0.15.0rc1-21-g32c5016' and '0.14.1'.

When the label passed to the drop method of a Series is not in the axis:
(a) if the index is not a MultiIndex then an exception is raised (in most cases; see issue #8530)
(b) if the index is a MultiIndex then an exception is not raised; the original series is returned

Examples of current behaviour:
(a)

>>> pd.Series([10, 20, 30], [1, 2, 3]).drop(7)

ValueError: labels [7] not contained in axis

(b)

>>> pd.Series(
    [10, 20, 30],
    pd.MultiIndex(
        levels=[[1, 2, 3], [4, 5, 6]],
        labels=[[0, 1, 2], [0, 1, 2]],
        names=['a', 'b'])
).drop(7, level='a')

a  b
1  4    10
2  5    20
3  6    30
dtype: int64

I propose that in (b) an exception be raised. Furthermore, an exception should be raised in (b) if (for instance) the label passed to drop had been 4, because although 4 is in the axis, it is not in the specified level.

The exception message in (b) would have to refer to the level, e.g.:

ValueError: labels [7] not contained in level

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions