Skip to content

REGR: Behavior differs when adding Series to frame between matching and non matching index #57462

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Fixed regressions
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`)
- Fixed regression in arithmetic methods not sorting columns when indexes are equal (:issue:`57462`)

.. ---------------------------------------------------------------------------
.. _whatsnew_221.bug_fixes:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9645,7 +9645,7 @@ def _align_series(
fdata = self._mgr
join_index = self.axes[1]
lidx, ridx = None, None
if not join_index.equals(other.index):
if not join_index.equals(other.index) or join == "outer":
join_index, lidx, ridx = join_index.join(
other.index, how=join, level=level, return_indexers=True
)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2108,3 +2108,15 @@ def test_mixed_col_index_dtype():
result = df1 + df2
expected = DataFrame(columns=list("abc"), data=1.0, index=[0])
tm.assert_frame_equal(result, expected)


def test_arithmetic_alignment():
df = DataFrame({"x": [], "a": []})
other = Series(dtype=float)
expected = DataFrame({"a": [], "x": []})
tm.assert_frame_equal(df + other, expected)

df = DataFrame({"x": [1], "a": [2]})
other = Series([3, 4], index=["x", "a"])
expected = DataFrame({"a": [6], "x": [4]})
tm.assert_frame_equal(df + other, expected)