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.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
cat_data = pd.Categorical([15, 16, 17, 18], categories=pd.Series(list(range(3, 24)), dtype="Int64"), ordered=True)
df1 = pd.DataFrame({"hr": cat_data, "values1": "a b c d".split()}).set_index("hr")
df2 = pd.DataFrame({"hr": cat_data, "values2": "xyzzy foo bar ...".split()}).set_index("hr")
df1.columns = pd.CategoricalIndex([4], dtype=cat_data.dtype, name="other_hr")
df2.columns = pd.CategoricalIndex([3], dtype=cat_data.dtype, name="other_hr")
print(pd.__version__)
# Clunky, but works
df_joined_1 = df1.reset_index(level="hr").merge(df2.reset_index(level="hr"), on="hr").set_index("hr")
# Works on 1.4.4 and nightly (3.0.0.dev0+2177.g8a1d5a06f9), not 2.2.3 or 2.3.0
df_joined_2 = df1.join(df2)
# returns True... assuming we got this far
df_joined_1.equals(df_joined_2)
Issue Description
join
ing two DataFrames which have a CategoricalIndex
as columns (for example, due to having pivoted on a categorical column) results in an InvalidIndexError (see below) on Pandas versions 2.2.3 and 2.3.0. The same code works with 1.4.4 (from which I am trying to migrate to 2.x) and nightly.
While the issue does not manifest in nightly, I am still reporting it in the hopes of getting it fixed in future 2.x releases.
Expected Behavior
Code executes successfully and the last statement returns True
Installed Versions
Pandas 1.4.4 (works)
Pandas 2.2.3 (does not work)
Pandas 2.3.0 (does not work)
Pandas nightly (works)
Error traceback
On Pandas 2.3.3:
Traceback (most recent call last):
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/indexes/base.py", line 3812, in get_loc
return self._engine.get_loc(casted_key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "pandas/_libs/index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 175, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index_class_helper.pxi", line 86, in pandas._libs.index.MaskedInt64Engine._check_type
KeyError: slice(None, None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/frame.py", line 10764, in join
return merge(
^^^^^^
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/reshape/merge.py", line 184, in merge
return op.get_result(copy=copy)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/reshape/merge.py", line 888, in get_result
result = self._reindex_and_concat(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/reshape/merge.py", line 837, in _reindex_and_concat
left = self.left[:]
~~~~~~~~~^^^
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/frame.py", line 4080, in __getitem__
and key in self.columns
^^^^^^^^^^^^^^^^^^^
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/indexes/category.py", line 368, in __contains__
return contains(self, key, container=self._engine)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/arrays/categorical.py", line 230, in contains
loc = cat.categories.get_loc(key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/voipiti/.pyenv/versions/pd23-312/lib/python3.12/site-packages/pandas/core/indexes/base.py", line 3818, in get_loc
raise InvalidIndexError(key)
pandas.errors.InvalidIndexError: slice(None, None, None)
Comment From: rhshadrach
Thanks for the report; I'll mark as a regression for now, but it seems likely the 2.x series will not see any more releases.
Comment From: jbrockmendel
i can't reproduce this in 2.3.0
Comment From: rhshadrach
I can reproduce on 2.3.x.