Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
[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
from datetime import datetime
data = dict(Date=[datetime(2024, 11, 1), datetime(2024, 11, 1), datetime(2024, 11, 2), datetime(2024, 11, 2)], sub=['a', 'b', 'c', 'd'], value1=[1,2,3,4], value2=[5,6,7,8])
df = pd.DataFrame(data)
pivot_table = df.pivot(index='sub', columns='Date', values=['value1', 'value2'])
df2 = pivot_table.reset_index()
# Checked following ways to locate column, but all failed
df2[df2.columns[0]]
# df2.loc[:, df2.columns[0]]
Issue Description
Hi,
I was transforming some dataframes and found that when there is NaT in multiIndex columns, I cannot locate that column but got keyError instead. The stack is below:
`
Traceback (most recent call last):
File "/Users/meg/opt/anaconda3/envs/py311/lib/python3.11/site-packages/pandas/core/indexes/multi.py", line 3053, in get_loc
return self._engine.get_loc(key)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "index.pyx", line 776, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc
File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 2152, in pandas._libs.hashtable.UInt64HashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 2176, in pandas._libs.hashtable.UInt64HashTable.get_item
KeyError: 33
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/meg/codes/leetcode/testpandas.py", line 11, in
df2[df2.columns[0]]
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
Issue Description
Hi,
I was transforming some dataframes and found that when there is NaT in multiIndex columns, I cannot locate that column but got keyError instead. The stack is below:
` Traceback (most recent call last): File "/Users/meg/opt/anaconda3/envs/py311/lib/python3.11/site-packages/pandas/core/indexes/multi.py", line 3053, in get_loc return self._engine.get_loc(key) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "index.pyx", line 776, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 2152, in pandas._libs.hashtable.UInt64HashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 2176, in pandas._libs.hashtable.UInt64HashTable.get_item KeyError: 33
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/Users/meg/codes/leetcode/testpandas.py", line 11, in
df2[df2.columns[0]]