Skip to content
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

This fix allows a DataFrame to retain key order from a dictionary #55696

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -437,9 +437,9 @@ Other
- Bug in :func:`cut` incorrectly allowing cutting of timezone-aware datetimes with timezone-naive bins (:issue:`54964`)
- Bug in :func:`infer_freq` and :meth:`DatetimeIndex.inferred_freq` with weekly frequencies and non-nanosecond resolutions (:issue:`55609`)
- Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55009`)
- Bug in :meth:`Dataframe.from_dict` which would always sort the rows of the created :class:`DataFrame`. (:issue:`55683`)
- Bug in rendering ``inf`` values inside a a :class:`DataFrame` with the ``use_inf_as_na`` option enabled (:issue:`55483`)
- 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`)
-

.. ***DO NOT USE THIS SECTION***

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)
5 changes: 2 additions & 3 deletions pandas/tests/frame/methods/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ def test_rename(self, float_frame):
# 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"]))
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
Loading