Skip to content

BUG: cannot set Series with reverse slicer #26939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
h-vetinari opened this issue Jun 19, 2019 · 2 comments · Fixed by #28131
Closed

BUG: cannot set Series with reverse slicer #26939

h-vetinari opened this issue Jun 19, 2019 · 2 comments · Fixed by #28131
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@h-vetinari
Copy link
Contributor

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.

@jbrockmendel jbrockmendel added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 23, 2019
@kmin-jeong
Copy link

can I try it?

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Aug 16, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants