Skip to content

BUG: inconsistent index order when creating DataFrame from dictionary with only 1 column #49233

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
3 tasks done
mwyschan opened this issue Oct 21, 2022 · 6 comments
Closed
3 tasks done
Assignees
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@mwyschan
Copy link

mwyschan commented Oct 21, 2022

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

pd.DataFrame({"a":{"b":1, "c":2, "d":3}})
#	a
# b	1
# c	2
# d	3

pd.DataFrame({"a":{"c":2, "d":3, "b":1}})
#	a
# b	1
# c	2
# d	3

pd.DataFrame({"a":{"c":2, "d":3, "b":1}, "e":{"b":4, "c":5, "d":6}})
#	a	e
# c	2	5
# d	3	6
# b	1	4

Issue Description

Not sure if related to this: #24859 (comment)
When creating a DataFrame from a nested dictionary with only 1 column, the order of index keys is not kept. i.e. pd.DataFrame({"a":{"c":2, "d":3, "b":1}}) should have index [c, d, b], not [b, c, d].

However, the order is kept when there is more than 1 column.

Expected Behavior

pd.DataFrame({"a":{"c":2, "d":3, "b":1}})
#	a
# c	2
# d	3
# b	1

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.11.1.el7.x86_64
Version : #1 SMP Fri Dec 18 16:34:56 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 65.4.0
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.5.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : 1.4.41
tables : None
tabulate : 0.8.10
xarray : 2022.9.0
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@mwyschan mwyschan added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 21, 2022
@mwyschan
Copy link
Author

Temporary workaround:

pd.DataFrame({"a": pd.Series({"c":2, "d":3, "b":1})})
#	a
# c	2
# d	3
# b	1

@vamsi-verma-s
Copy link
Contributor

This sorting of index when one index is present is happening in union_indexes
I am not sure why if there is only 1 index it needs to be sorted.

def union_indexes(indexes, sort: bool | None = True) -> Index:
"""
Return the union of indexes.
The behavior of sort and names is not consistent.
Parameters
----------
indexes : list of Index or list objects
sort : bool, default True
Whether the result index should come out sorted or not.
Returns
-------
Index
"""
if len(indexes) == 0:
raise AssertionError("Must have at least 1 Index to union")
if len(indexes) == 1:
result = indexes[0]
if isinstance(result, list):
result = Index(sorted(result))
return result

@codestorm177
Copy link

take

darren4 pushed a commit to codestorm177/panda-dev-local that referenced this issue Dec 11, 2022
… with only 1 column pandas-dev#49233

Removed sorted function for when the dataframe constructor is called with a python dictionary so the ordering does not surprise users. A test to verify this was also added.
darren4 pushed a commit to codestorm177/panda-dev-local that referenced this issue Dec 11, 2022
… with only 1 column pandas-dev#49233

add style fixes for previous commit
darren4 pushed a commit to codestorm177/panda-dev-local that referenced this issue Dec 11, 2022
… with only 1 column pandas-dev#49233

added fix to still complete sort when the sort param is true
@yuanx749
Copy link
Contributor

I try to fix it but it breaks the existing test. It seems the sorted behavior is expected according to the test below:

# index
data = {"A": {"foo": 0, "bar": 1}}
# gets sorted alphabetical
df = DataFrame(data)
renamed = df.rename(index={"foo": "bar", "bar": "foo"})
tm.assert_index_equal(renamed.index, Index(["foo", "bar"]))
renamed = df.rename(index=str.upper)
tm.assert_index_equal(renamed.index, Index(["BAR", "FOO"]))

@simonjayhawkins simonjayhawkins added the Constructors Series/DataFrame/Index/pd.array Constructors label Feb 6, 2024
@yuanx749
Copy link
Contributor

Seems this issue has been solved in #55696.

@mroeschke
Copy link
Member

Closed by #55696

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

No branches or pull requests

6 participants