BUG: The difference in behaviour of indexing between 1.2.4 and 1.3.0 · Issue #42461 · pandas-dev/pandas (original) (raw)
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of pandas.
- (optional) I have confirmed this bug exists on the master branch of pandas.
I have some code that I used to extract the index columns as data frame, which worked in pandas 1.2.4 but not in 1.3.0. I have read what's new (https://pandas.pydata.org/pandas-docs/version/1.3.0/whatsnew/v1.3.0.html). To my best knowledge, this seems to be an undocumented change. I wonder if I can get some advice on how to make the code work for both 1.2 and 1.3 and potentially 1.4.
Code Sample, a copy-pastable example
To extract the index columns
col = df.reset_index()[df.index.names[:]]
Problem description
Say if we have a data frame.
d = {'col1': [1, 2], 'col2': [3, 4]} df = pd.DataFrame(data=d) df = df.set_index('col1') df col2 col1
1 3 2 4
Expected Output
For pandas == 1.2.4,
df.reset_index()[df.index.names[:]] col1 0 1 1 2
For pandas >= 1.3.0,
df.reset_index()[df.index.names[:]] Traceback (most recent call last): File "", line 1, in File "
/GitHub/pandas/pandas/core/frame.py", line 3444, in getitem if com.is_bool_indexer(key): File "/GitHub/pandas/pandas/core/common.py", line 147, in is_bool_indexer return len(key) > 0 and lib.is_bool_list(key) TypeError: Argument 'obj' has incorrect type (expected list, got FrozenList)