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: df.to_dict(orient='tight') raises UserWarning incorrectly for duplicate columns #58281

Closed
3 tasks done
Cerebex opened this issue Apr 16, 2024 · 3 comments · Fixed by #58335
Closed
3 tasks done

BUG: df.to_dict(orient='tight') raises UserWarning incorrectly for duplicate columns #58281

Cerebex opened this issue Apr 16, 2024 · 3 comments · Fixed by #58335
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@Cerebex
Copy link

Cerebex commented Apr 16, 2024

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
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'A': [7, 8, 9]
})
df.to_dict(orient='tight')

Issue Description

If I have a pandas dataframe with duplicate column names and I use the method df.to_dict(orient='tight') it throws the following UserWarning:

"DataFrame columns are not unique, some columns will be omitted."

This error makes sense for creating dictionary from a pandas dataframe with columns as dictionary keys but not in the tight orientation where columns are placed into a columns list value.

It should not throw this warning at all with orient='tight'.

Expected Behavior

Do not throw UserWarning when there are duplicate column names in a pandas dataframe if the following parameter is set: df.to_dict(oreint='tight')

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.11.4.final.0
python-bits : 64
OS : Darwin
OS-release : 23.4.0
Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 68.2.0
pip : 23.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.2.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@Cerebex Cerebex added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 16, 2024
@Aloqeely
Copy link
Contributor

Aloqeely commented Apr 18, 2024

Thanks for the report! Your example doesn't seem to be reproducing this warning because df will end up having only 2 unique columns A and B

But yes, the warning is triggered when the DataFrame has duplicate columns and I agree that shouldn't be the case when orient='tight'. PRs to fix this are welcome!

@luke396
Copy link
Contributor

luke396 commented Apr 20, 2024

take

@luke396
Copy link
Contributor

luke396 commented Apr 20, 2024

A example will reproduce the warning

import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
})
df.columns = ['A', 'A']

df.to_dict(orient='tight')
/tmp/ipykernel_29817/4077745780.py:8: UserWarning: DataFrame columns are not unique, some columns will be omitted.
  df.to_dict(orient='tight')

{'index': [0, 1, 2],
 'columns': ['A', 'A'],
 'data': [[1, 4], [2, 5], [3, 6]],
 'index_names': [None],
 'column_names': [None]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants