Skip to content

Commit f605da2

Browse files
committed
This fix allows a DataFrame to retain key order from a dictionary with only one column instead of sorting them alphabetically.
1 parent 074ab2f commit f605da2

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ Datetimelike
327327
- Bug in addition or subtraction of :class:`BusinessDay` offset with ``offset`` attribute to non-nanosecond :class:`Index`, :class:`Series`, or :class:`DataFrame` column giving incorrect results (:issue:`55608`)
328328
- Bug in addition or subtraction of :class:`DateOffset` objects with microsecond components to ``datetime64`` :class:`Index`, :class:`Series`, or :class:`DataFrame` columns with non-nanosecond resolution (:issue:`55595`)
329329
- Bug in addition or subtraction of very large :class:`Tick` objects with :class:`Timestamp` or :class:`Timedelta` objects raising ``OverflowError`` instead of ``OutOfBoundsTimedelta`` (:issue:`55503`)
330-
-
330+
- Bug in creating a Dataframe using the from_dict method which would alphabetize the rows of the created DataFrame. (:issue:`55683`)
331331

332332
Timedelta
333333
^^^^^^^^^

pandas/core/indexes/api.py

Lines changed: 4 additions & 1 deletion
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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ def test_rename(self, float_frame):
5353
# gets sorted alphabetical
5454
df = DataFrame(data)
5555
renamed = df.rename(index={"foo": "bar", "bar": "foo"})
56-
tm.assert_index_equal(renamed.index, Index(["foo", "bar"]))
56+
tm.assert_index_equal(renamed.index, Index(["bar", "foo"]))
5757

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

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

pandas/tests/indexing/multiindex/test_setitem.py

Lines changed: 1 addition & 1 deletion
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)