Skip to content

BUG: Inconsistent Sorting using DataFrame.from_dict() method #55683

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

Closed
2 of 3 tasks
themox opened this issue Oct 25, 2023 · 3 comments · Fixed by #55696
Closed
2 of 3 tasks

BUG: Inconsistent Sorting using DataFrame.from_dict() method #55683

themox opened this issue Oct 25, 2023 · 3 comments · Fixed by #55696
Labels
Bug DataFrame DataFrame data structure IO Data IO issues that don't fit into a more specific label

Comments

@themox
Copy link

themox commented Oct 25, 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

print(pd.__version__)

d1 = {
    "value2": 123,
    "value1": 532,
    "animal": 222,
    "plant": False,
    "name": "bunny"
}

d2 = {
    "value2": 321,
    "value1": 235,
    "animal": 555,
    "plant": False,
    "name": "lamb"
}

d3 = {
    "value2": 777,
    "value1": 999,
    "animal": 333,
    "plant": True,
    "name": "rosebush"
}

combined = {}

combined["alpha"] = d1
combined["bravo"] = d2
combined["charlie"] = d3

combined1 = {}
combined1["alpha"] = d1

# All four of these situations should produce their keys in the same order...
test_df = pd.DataFrame.from_dict(data=combined, orient = "columns")
print(test_df.to_markdown())

print("")

#this one is different; it only has one column in final so gets sorted alphabetically by  row headers
test_df1 = pd.DataFrame.from_dict(data=combined1, orient = "columns")
print(test_df1.to_markdown())

print("")

test_df = pd.DataFrame.from_dict(data=combined, orient="index")
test_df = test_df.T
print(test_df.to_markdown())

print("")

test_df1 = pd.DataFrame.from_dict(data=combined1, orient="index")
test_df1 = test_df1.T
print(test_df1.to_markdown())

# Recommendation 1:
# all should produce the same behavior, whatever that is.  
# In particular, orient = columns, should produce same order whether there are 1 data column or many.

# Recommendation 2:
# add a keyword argument for sort in the pandas.DataFrame.from_dict method so that user can decide at 
# run time which sorting they'd like

# reproducible in pandas 2.0.3, 2.1.1

Issue Description

When using the pandas.DataFrame.from_dict() method to create a data frame, function produces different results when using orient="columns" depending on whether the number of indices is 1 or more. If index count == 1, columns will be sorted alphabetically. If index count >= 2, columns will be sorted as provided in the dict.

Expected Behavior

Expected behavior should be that, regardless of the index count, the .from_dict() method should produce columns in the same order every time. Even better would be to provide a sort argument to allow the user to sort in a certain way when they call the function. Barring that, choose a way and function should perform that same way every time. Recommend using "as provided" as default sorting (i.e. no sorting); this is the same behavior as from_dict(orient="index") and would give consistency across the function.

Installed Versions

INSTALLED VERSIONS

commit : e86ed37
python : 3.10.12.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Fri Sep 15 13:41:28 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.1
numpy : 1.25.1
pytz : 2023.3
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 : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : 0.19.0
tzdata : 2023.3
qtpy : None
pyqt5 : None
None

@themox themox added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 25, 2023
@paulreece
Copy link
Contributor

paulreece commented Oct 25, 2023

Thanks for submitting the issue @themox . Can you add a description of the bug after BUG: in the title?

I have a PR coming to fix this.

@paulreece paulreece added DataFrame DataFrame data structure and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 25, 2023
@rhshadrach
Copy link
Member

Even better would be to provide a sort argument to allow the user to sort in a certain way when they call the function.

It seems to me users can sort as they like after the DataFrame is constructed.

@rhshadrach rhshadrach added the IO Data IO issues that don't fit into a more specific label label Oct 26, 2023
@themox themox changed the title BUG: BUG: Inconsistent Sorting using DataFrame.from_dict() method Oct 26, 2023
@themox
Copy link
Author

themox commented Oct 26, 2023

Even better would be to provide a sort argument to allow the user to sort in a certain way when they call the function.

It seems to me users can sort as they like after the DataFrame is constructed.

This is absolutely true, and I would not fault you for that design decision. Biggest concern here is the inconsistent behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug DataFrame DataFrame data structure IO Data IO issues that don't fit into a more specific label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants