Skip to content

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

Closed
@mwyschan

Description

@mwyschan

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

Metadata

Metadata

Assignees

Labels

BugConstructorsSeries/DataFrame/Index/pd.array ConstructorsNeeds TriageIssue that has not been reviewed by a pandas team member

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions