diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 2708e69d01224..cfc10485f3dad 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1234,12 +1234,12 @@ Other - Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`) - Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`) - Deprecated the keyword ``check_datetimelike_compat`` in :meth:`testing.assert_frame_equal` and :meth:`testing.assert_series_equal` (:issue:`55638`) +- Fixed bug in :meth:`DataFrame.combine` with non-unique columns (:issue:`51340`) - Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when trying to replace :class:`NA` values in a :class:`Float64Dtype` object with ``np.nan``; this now works with ``pd.set_option("mode.nan_is_na", False)`` and is irrelevant otherwise (:issue:`55127`) - Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when trying to replace :class:`np.nan` values in a :class:`Int64Dtype` object with :class:`NA`; this is now a no-op with ``pd.set_option("mode.nan_is_na", False)`` and is irrelevant otherwise (:issue:`51237`) - Fixed bug in the :meth:`Series.rank` with object dtype and extremely small float values (:issue:`62036`) - Fixed bug where the :class:`DataFrame` constructor misclassified array-like objects with a ``.name`` attribute as :class:`Series` or :class:`Index` (:issue:`61443`) - Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`) - .. ***DO NOT USE THIS SECTION*** - diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9c41b82bbbc8e..f8d6366180a98 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -9041,9 +9041,9 @@ def combine( new_columns = self.columns.union(other_columns, sort=False) do_fill = fill_value is not None result = {} - for col in new_columns: - series = this[col] - other_series = other[col] + for i, col in enumerate(new_columns): + series = this.iloc[:, i] + other_series = other.iloc[:, i] this_dtype = series.dtype other_dtype = other_series.dtype diff --git a/pandas/tests/frame/methods/test_combine.py b/pandas/tests/frame/methods/test_combine.py index bc6a67e4e1f32..abc8fda8fb88d 100644 --- a/pandas/tests/frame/methods/test_combine.py +++ b/pandas/tests/frame/methods/test_combine.py @@ -45,3 +45,17 @@ def test_combine_generic(self, float_frame): ) tm.assert_frame_equal(chunk, exp) tm.assert_frame_equal(chunk2, exp) + + def test_combine_non_unique_columns(self): + # GH#51340 + df = pd.DataFrame({"A": range(5), "B": range(5)}) + df.columns = ["A", "A"] + + other = df.copy() + df.iloc[1, :] = 11 + + def combiner(a, b): + return b + + result = df.combine(other, combiner) + tm.assert_frame_equal(result, other)