Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] 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
Comment From: CloseChoice
Please note that
s.le(t)
#0 True
#1 True
#2 True
#3 True
#4 True
#dtype: bool
works fine and gives the expected result. I would naively think that logical comparisons match the behaviour of the overloaded double-underscore methods. The documentation also states this equivalence:
Equivalent to series <= other, but with support to substitute a fill_value for missing data in either one of the inputs.
Edit: This is no regression, at least until v1.1.0
Edit2: Also note that the exact same behaviour appears for <, >, >=, ==, !=
while their counterparts .lt, .gt, .ge, .eq, .ne
all work fine.
Comment From: Dr-Irv
Broadcasting also doesn't work for comparisons and is inconsistent as well:
>>> s = pd.Series([1,2,3,4], index=pd.MultiIndex.from_product([["a", "b"], ["x", "y"]], names=["ab", "xy"]))
>>> s
ab xy
a x 1
y 2
b x 3
y 4
dtype: int64
>>> t = pd.Series([0, 5], index=pd.Index(["a", "b"], name="ab"))
>>> t
ab
a 0
b 5
dtype: int64
>>> t < s
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 48, in __lt__
return self._cmp_method(other, operator.lt)
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
>>> t.lt(s)
ab xy
a x True
y True
b x False
y False
dtype: bool
Comment From: simonjayhawkins
somewhat related https://github.com/pandas-dev/pandas/issues/36941#issuecomment-705004487 and https://github.com/pandas-dev/pandas/issues/20442#issuecomment-375116149
it maybe that we have tests for the None
case that break when sharing code.
since the ValueError("Can only compare identically-labeled Series objects")
is explicitly raised, the issue in the OP could be considered not a bug but an enhancement request.
so we could add more logic to do the alignment in the existing code (-1). Or share the working code so that it is consistent (+1).
If the latter, I think can probably close as a duplicate.
Comment From: Dr-Irv
so we could add more logic to do the alignment in the existing code (-1). Or share the working code so that it is consistent (+1).
I think we should do the alignment. As shown in https://github.com/pandas-dev/pandas/issues/47554#issuecomment-1171592691 , the difference in behavior between Series.le()
and Series.__le__()
seems inconsistent
Comment From: kathryn1229
take