Skip to content
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 (Regression): Using a tuple as first column name in empty dataframe creates a non functional dataframe and memory errors #54385

Open
2 of 3 tasks
jenno-verdonck opened this issue Aug 3, 2023 · 4 comments
Labels
Bug DataFrame DataFrame data structure Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@jenno-verdonck
Copy link

jenno-verdonck commented Aug 3, 2023

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

@jenno-verdonck jenno-verdonck added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 3, 2023
@jenno-verdonck jenno-verdonck changed the title BUG: Using a tuple as first column name in empty dataframe creates a non functional dataframe BUG: Using a tuple as first column name in empty dataframe creates a non functional dataframe and memory errors Aug 4, 2023
@phofl phofl added DataFrame DataFrame data structure Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 6, 2023
@jenno-verdonck
Copy link
Author

This seems to be a regression.
Running this in version 2.0.0 results in an InvalidIndexError.
Running this in version 1.5.3 works as expected without any errors.

@jenno-verdonck jenno-verdonck changed the title BUG: Using a tuple as first column name in empty dataframe creates a non functional dataframe and memory errors BUG (Regression): Using a tuple as first column name in empty dataframe creates a non functional dataframe and memory errors Aug 18, 2023
@Tialo
Copy link

Tialo commented Aug 25, 2023

I also experienced it.

import pandas as pd

df = pd.DataFrame([1, 2, 3])
df[0, 0] = 4

if you will try to print this dataframe it will fail with this error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/frame.py", line 1094, in __repr__
    return self.to_string(**repr_params)
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/frame.py", line 1271, in to_string
    return fmt.DataFrameRenderer(formatter).to_string(
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/format.py", line 1134, in to_string
    string = string_formatter.to_string()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/string.py", line 30, in to_string
    text = self._get_string_representation()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/string.py", line 45, in _get_string_representation
    strcols = self._get_strcols()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/string.py", line 36, in _get_strcols
    strcols = self.fmt.get_strcols()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/format.py", line 615, in get_strcols
    strcols = self._get_strcols_without_index()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/format.py", line 870, in _get_strcols_without_index
    str_columns = self._get_formatted_column_labels(self.tr_frame)
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/format.py", line 949, in _get_formatted_column_labels
    dtypes = self.frame.dtypes
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/generic.py", line 6159, in dtypes
    return self._constructor_sliced(data, index=self._info_axis, dtype=np.object_)
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/series.py", line 500, in __init__
    com.require_length_match(data, index)
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/common.py", line 576, in require_length_match
    raise ValueError(
ValueError: Length of values (2) does not match length of index (3)

But if you set another index

import pandas as pd

df = pd.DataFrame([1, 2, 3])
df[1, 0] = 4

print will fail with another exception

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/frame.py", line 1094, in __repr__
    return self.to_string(**repr_params)
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/frame.py", line 1271, in to_string
    return fmt.DataFrameRenderer(formatter).to_string(
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/format.py", line 1134, in to_string
    string = string_formatter.to_string()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/string.py", line 30, in to_string
    text = self._get_string_representation()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/string.py", line 45, in _get_string_representation
    strcols = self._get_strcols()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/string.py", line 36, in _get_strcols
    strcols = self.fmt.get_strcols()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/format.py", line 615, in get_strcols
    strcols = self._get_strcols_without_index()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/format.py", line 870, in _get_strcols_without_index
    str_columns = self._get_formatted_column_labels(self.tr_frame)
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/io/formats/format.py", line 949, in _get_formatted_column_labels
    dtypes = self.frame.dtypes
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/generic.py", line 6158, in dtypes
    data = self._mgr.get_dtypes()
  File "/Users/y/PycharmProjects/tamli/.venv/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 278, in get_dtypes
    return dtypes.take(self.blknos)
IndexError: index 3 is out of bounds for axis 0 with size 3

What is really important here, that exception does not occur while setting values, but only while working with the dataframe.

@jenno-verdonck
Copy link
Author

I get the same ValueError in both cases using python 3.9.

It seems that the double index is split into 2 columns while only having values for 1. This results in ValueErrors when trying to print the dataframe with undefined values. Your second example (df[1,0]) reveals to me that the assignment is only done on the first given index. Displaying df[1] seems to work and returns a series containing only the value 4. Displaying the full dataframe results in ValueError while displaying df[0] causes python to fully crash without any exceptions. Eventviewer on windows does shows an application error saying that python.exe failed with Exception code: 0xc0000005.

@jenno-verdonck
Copy link
Author

The bug still exists in the latest python and pandas release. Especially the memory errors seem concerning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug DataFrame DataFrame data structure Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
Development

No branches or pull requests

3 participants