Skip to content

BUG: RangeIndex has zero length after subtraction from constant value #53255

Closed
@mosc9575

Description

@mosc9575

Pandas version checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas.
  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
s = pd.Series([0,1,2,3])
print(s.index)
>>> RangeIndex(start=0, stop=4, step=1)
print(-s.index)
>>> Index([0, -1, -2, -3], dtype='int64')
print(3-s.index)
>>> RangeIndex(start=3, stop=-1, step=1)
print(len(3-s.index))
>>> 0

Issue Description

If the index of a Series or DataFrame is a RangeIndex object the example below

s = pd.Series([0,1,2,3])
s.index = s.index.max() - s.index

raises a ValueError, because the new RangeIndex has a length of zero.

ValueError Traceback
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)
Input In [26], in <cell line: 2>()
      1 s = pd.Series([0,1,2,3])
----> 2 s.index = s.index.max() - s.index

File /opt/conda/envs/bk3/lib/python3.9/site-packages/pandas/core/generic.py:6002, in NDFrame.__setattr__(self, name, value)
   6000 try:
   6001     object.__getattribute__(self, name)
-> 6002     return object.__setattr__(self, name, value)
   6003 except AttributeError:
   6004     pass

File /opt/conda/envs/bk3/lib/python3.9/site-packages/pandas/_libs/properties.pyx:69, in pandas._libs.properties.AxisProperty.__set__()

File /opt/conda/envs/bk3/lib/python3.9/site-packages/pandas/core/generic.py:730, in NDFrame._set_axis(self, axis, labels)
    725 """
    726 This is called from the cython code when we set the `index` attribute
    727 directly, e.g. `series.index = [1, 2, 3]`.
    728 """
    729 labels = ensure_index(labels)
--> 730 self._mgr.set_axis(axis, labels)
    731 self._clear_item_cache()

File /opt/conda/envs/bk3/lib/python3.9/site-packages/pandas/core/internals/managers.py:225, in BaseBlockManager.set_axis(self, axis, new_labels)
    223 def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
    224     # Caller is responsible for ensuring we have an Index object.
--> 225     self._validate_set_axis(axis, new_labels)
    226     self.axes[axis] = new_labels

File /opt/conda/envs/bk3/lib/python3.9/site-packages/pandas/core/internals/base.py:70, in DataManager._validate_set_axis(self, axis, new_labels)
     67     pass
     69 elif new_len != old_len:
---> 70     raise ValueError(
     71         f"Length mismatch: Expected axis has {old_len} elements, new "
     72         f"values have {new_len} elements"
     73     )

ValueError: Length mismatch: Expected axis has 4 elements, new values have 0 elements

Expected Behavior

I would expect, that the step changes to -1 or the object is transferred to an Index object like -s.index does.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 37ea63d python : 3.9.10.final.0 python-bits : 64 OS : Linux OS-release : 4.19.0-21-amd64 Version : #1 SMP Debian 4.19.249-2 (2022-06-30) machine : x86_64 processor : x86_64 byteorder : little LC_ALL : en_US.UTF-8 LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.0.1
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
setuptools : 60.9.3
pip : 22.0.3
Cython : 0.29.28
pytest : 7.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.1.1
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.02.0
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexRelated to the Index class or subclassesNumeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions