Skip to content

BUG: cannot set Series with reverse slicer #26939

Closed
@h-vetinari

Description

@h-vetinari

Minimal example:

>>> import pandas as pd
>>> s = pd.Series(index=range(2010, 2020))
>>> s.loc[2015:2010:-1] = [6, 5, 4, 3, 2, 1]
Traceback (most recent call last):
[...]
ValueError: cannot set using a slice indexer with a different length than the value

I see no reason why this shouldn't work, as setting with the forward slicer works without problems, and getting with the reverse slicer also works without issue:

>>> # turn list around because slicer is (not) reversed compared to above
>>> s.loc[2010:2015] = [6, 5, 4, 3, 2, 1][::-1]
>>> s
2010    1.0
2011    2.0
2012    3.0
2013    4.0
2014    5.0
2015    6.0
2016    NaN
2017    NaN
2018    NaN
2019    NaN
dtype: float64
>>> s.loc[2015:2010:-1] == [6, 5, 4, 3, 2, 1]  # comparison, not assignment
2015    True
2014    True
2013    True
2012    True
2011    True
2010    True
dtype: bool

PS: For the failure, it does not matter if the RHS is a np.array, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated 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