Description
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
import pandas as pd
df1 = pd.DataFrame()
df1[(0, 0)] = [1, 2, 3]
df2 = pd.DataFrame()
df2['a'] = [1, 2, 3]
df2[(0, 0)] = [4, 5, 6]
print(df2.columns)
print(f'df2=\n{df2}')
print(df1.columns)
print(f'df1=\n{df1}')
Issue Description
A dataframe becomes completely unusable when the first column was assigned using a tuple as key. (example df1)
The tuple gets transformed in 2 different columns while data for only one is given. This cause print statements to return "ValueError: Length of values (1) does not match length of index (2)".
When a dataframe already has an existing column (example df2) the assignment works as expected.
Running the assignment on the df1 dataframe 5 times results in a wide range of memory related errors such as:
malloc(): unaligned tcache chunk detected
IndexError: index 94573497622112 is out of bounds for axis 0 with size 5
corrupted size vs. prev_size
double free or corruption (out)
Expected Behavior
The assignment should behave the same in both examples and use the tuple as single header.
Installed Versions
INSTALLED VERSIONS
commit : 0f43794
python : 3.10.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-50-generic
Version : #50-Ubuntu SMP PREEMPT_DYNAMIC Mon Jul 10 18:24:29 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.0.3
numpy : 1.25.1
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : 2.3.1
pyqt5 : None