Skip to content

Commit 73e085e

Browse files
authored
This fix allows a DataFrame to retain key order from a dictionary (#55696)
This fix allows a DataFrame to retain key order from a dictionary with only one column instead of sorting them alphabetically.
1 parent 1d20b87 commit 73e085e

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ Other
444444
- Bug in :func:`cut` incorrectly allowing cutting of timezone-aware datetimes with timezone-naive bins (:issue:`54964`)
445445
- Bug in :func:`infer_freq` and :meth:`DatetimeIndex.inferred_freq` with weekly frequencies and non-nanosecond resolutions (:issue:`55609`)
446446
- Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55009`)
447+
- Bug in :meth:`Dataframe.from_dict` which would always sort the rows of the created :class:`DataFrame`. (:issue:`55683`)
447448
- Bug in rendering ``inf`` values inside a a :class:`DataFrame` with the ``use_inf_as_na`` option enabled (:issue:`55483`)
448449
- Bug in rendering a :class:`Series` with a :class:`MultiIndex` when one of the index level's names is 0 not having that name displayed (:issue:`55415`)
449-
-
450450

451451
.. ***DO NOT USE THIS SECTION***
452452

pandas/core/indexes/api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ def union_indexes(indexes, sort: bool | None = True) -> Index:
220220
if len(indexes) == 1:
221221
result = indexes[0]
222222
if isinstance(result, list):
223-
result = Index(sorted(result))
223+
if not sort:
224+
result = Index(result)
225+
else:
226+
result = Index(sorted(result))
224227
return result
225228

226229
indexes, kind = _sanitize_and_check(indexes)

pandas/tests/frame/constructors/test_from_dict.py

+21
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,24 @@ def test_from_dict_orient_invalid(self):
200200
)
201201
with pytest.raises(ValueError, match=msg):
202202
DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient="abc")
203+
204+
def test_from_dict_order_with_single_column(self):
205+
data = {
206+
"alpha": {
207+
"value2": 123,
208+
"value1": 532,
209+
"animal": 222,
210+
"plant": False,
211+
"name": "test",
212+
}
213+
}
214+
result = DataFrame.from_dict(
215+
data,
216+
orient="columns",
217+
)
218+
expected = DataFrame(
219+
[[123], [532], [222], [False], ["test"]],
220+
index=["value2", "value1", "animal", "plant", "name"],
221+
columns=["alpha"],
222+
)
223+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/methods/test_rename.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ def test_rename(self, float_frame):
5050
# index
5151
data = {"A": {"foo": 0, "bar": 1}}
5252

53-
# gets sorted alphabetical
5453
df = DataFrame(data)
5554
renamed = df.rename(index={"foo": "bar", "bar": "foo"})
56-
tm.assert_index_equal(renamed.index, Index(["foo", "bar"]))
55+
tm.assert_index_equal(renamed.index, Index(["bar", "foo"]))
5756

5857
renamed = df.rename(index=str.upper)
59-
tm.assert_index_equal(renamed.index, Index(["BAR", "FOO"]))
58+
tm.assert_index_equal(renamed.index, Index(["FOO", "BAR"]))
6059

6160
# have to pass something
6261
with pytest.raises(TypeError, match="must pass an index to rename"):

pandas/tests/indexing/multiindex/test_setitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_multiindex_setitem2(self):
175175
)
176176

177177
expected = df_orig.copy()
178-
expected.iloc[[0, 2, 3]] *= 2
178+
expected.iloc[[0, 1, 3]] *= 2
179179

180180
idx = pd.IndexSlice
181181
df = df_orig.copy()

0 commit comments

Comments
 (0)