Skip to content

Commit

Permalink
This fix allows a DataFrame to retain key order from a dictionary wit…
Browse files Browse the repository at this point in the history
…h only one column instead of sorting them alphabetically.
  • Loading branch information
paulreece committed Oct 26, 2023
1 parent 074ab2f commit 026e140
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ Datetimelike
- 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`)
- 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`)
- 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`)
-
- Bug in creating a Dataframe using the from_dict method which would alphabetize the rows of the created DataFrame. (:issue:`55683`)

Timedelta
^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ def union_indexes(indexes, sort: bool | None = True) -> Index:
if len(indexes) == 1:
result = indexes[0]
if isinstance(result, list):
result = Index(sorted(result))
if not sort:
result = Index(result)
else:
result = Index(sorted(result))
return result

indexes, kind = _sanitize_and_check(indexes)
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/frame/constructors/test_from_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,24 @@ def test_from_dict_orient_invalid(self):
)
with pytest.raises(ValueError, match=msg):
DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient="abc")

def test_from_dict_order_with_single_column(self):
data = {
"alpha": {
"value2": 123,
"value1": 532,
"animal": 222,
"plant": False,
"name": "test",
}
}
result = DataFrame.from_dict(
data,
orient="columns",
)
expected = DataFrame(
[[123], [532], [222], [False], ["test"]],
index=["value2", "value1", "animal", "plant", "name"],
columns=["alpha"],
)
tm.assert_frame_equal(result, expected)
4 changes: 2 additions & 2 deletions pandas/tests/frame/methods/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def test_rename(self, float_frame):
# gets sorted alphabetical
df = DataFrame(data)
renamed = df.rename(index={"foo": "bar", "bar": "foo"})
tm.assert_index_equal(renamed.index, Index(["foo", "bar"]))
tm.assert_index_equal(renamed.index, Index(["bar", "foo"]))

renamed = df.rename(index=str.upper)
tm.assert_index_equal(renamed.index, Index(["BAR", "FOO"]))
tm.assert_index_equal(renamed.index, Index(["FOO", "BAR"]))

# have to pass something
with pytest.raises(TypeError, match="must pass an index to rename"):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/multiindex/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def test_multiindex_setitem2(self):
)

expected = df_orig.copy()
expected.iloc[[0, 2, 3]] *= 2
expected.iloc[[0, 1, 3]] *= 2

idx = pd.IndexSlice
df = df_orig.copy()
Expand Down

0 comments on commit 026e140

Please sign in to comment.