Skip to content

BUG: Series with same index values, but in different orders, cannot be compared, but can be added #47554

Open
@Dr-Irv

Description

@Dr-Irv

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([1,2,3,4,5])
s
t=s[-1::-1]
t
s + t
s <= t

Issue Description

Code above produces

>>> import pandas as pd
>>> s=pd.Series([1,2,3,4,5])
>>> s
0    1
1    2
2    3
3    4
4    5
dtype: int64
>>> t=s[-1::-1]
>>> t
4    5
3    4
2    3
1    2
0    1
dtype: int64
>>> s + t
0     2
1     4
2     6
3     8
4    10
dtype: int64
>>> s <= t
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\ops\common.py", line 70, in new_method
    return method(self, other)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\arraylike.py", line 52, in __le__
    return self._cmp_method(other, operator.le)
  File "C:\Anaconda3\envs\pandasstubs\lib\site-packages\pandas\core\series.py", line 5617, in _cmp_method
    raise ValueError("Can only compare identically-labeled Series objects")
ValueError: Can only compare identically-labeled Series objects

Expected Behavior

s<=t should produce

0    True
1    True
2    True
3    True
4    True
dtype: bool

Question is why we align the indices in arithmetic, but not in a logical operation. The two series are identically labeled, but just not in the same order, so the error message is misleading.

Installed Versions

INSTALLED VERSIONS ------------------ commit : f4ca4d3 python : 3.8.12.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19043 machine : AMD64 processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252

pandas : 1.5.0.dev0+1050.gf4ca4d3d0e
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 60.9.3
pip : 22.0.3
Cython : 0.29.30
pytest : 7.1.2
hypothesis : 6.46.11
sphinx : 4.3.2
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.8.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.4
brotli :
fastparquet : 0.8.1
fsspec : 2021.11.0
gcsfs : 2021.11.0
matplotlib : 3.5.1
numba : 0.53.1
numexpr : 2.8.0
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : 1.1.7
pyxlsb : None
s3fs : 2021.11.0
scipy : 1.8.1
snappy :
sqlalchemy : 1.4.37
tables : 3.7.0
tabulate : 0.8.9
xarray : 2022.3.0
xlrd : 2.0.1
xlwt : 1.3.0
zstandard : None

Metadata

Metadata

Assignees

Labels

API - ConsistencyInternal Consistency of API/BehaviorBugIndexingRelated to indexing on series/frames, not to indexes themselvesNumeric OperationsArithmetic, Comparison, and Logical operationsSeriesSeries data structure

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions