-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
BUG: Series with same index values, but in different orders, cannot be compared, but can be added #47554
Comments
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:
Edit: This is no regression, at least until v1.1.0 Edit2: Also note that the exact same behaviour appears for |
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 |
somewhat related #36941 (comment) and #20442 (comment) it maybe that we have tests for the since the 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. |
I think we should do the alignment. As shown in #47554 (comment) , the difference in behavior between |
take |
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
Issue Description
Code above produces
Expected Behavior
s<=t
should produceQuestion 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
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
The text was updated successfully, but these errors were encountered: