From 4375aa9933f5d55b970e3f94db86886c722b7219 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 30 Oct 2025 18:11:10 -0700 Subject: [PATCH] BUG: Series.combine_first with reindex None->Nan --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/series.py | 4 ---- pandas/tests/series/methods/test_combine_first.py | 9 +++++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 38755aef32b85..e14fb52951713 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1187,6 +1187,7 @@ Reshaping - Bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`) - Bug in :meth:`DataFrame.join` when a :class:`DataFrame` with a :class:`MultiIndex` would raise an ``AssertionError`` when :attr:`MultiIndex.names` contained ``None``. (:issue:`58721`) - Bug in :meth:`DataFrame.merge` where merging on a column containing only ``NaN`` values resulted in an out-of-bounds array access (:issue:`59421`) +- Bug in :meth:`Series.combine_first` incorrectly replacing ``None`` entries with ``NaN`` (:issue:`58977`) - Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`) - Bug in :meth:`DataFrame.unstack` raising an error with indexes containing ``NaN`` with ``sort=False`` (:issue:`61221`) - Bug in :meth:`DataFrame.merge` when merging two :class:`DataFrame` on ``intc`` or ``uintc`` types on Windows (:issue:`60091`, :issue:`58713`) diff --git a/pandas/core/series.py b/pandas/core/series.py index fe71a3ab91933..5e1686b515e8a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -87,7 +87,6 @@ ) from pandas.core.dtypes.dtypes import ( ExtensionDtype, - SparseDtype, ) from pandas.core.dtypes.generic import ( ABCDataFrame, @@ -3254,9 +3253,6 @@ def combine_first(self, other) -> Series: if self.dtype == other.dtype: if self.index.equals(other.index): return self.mask(self.isna(), other) - elif self._can_hold_na and not isinstance(self.dtype, SparseDtype): - this, other = self.align(other, join="outer") - return this.mask(this.isna(), other) new_index = self.index.union(other.index) diff --git a/pandas/tests/series/methods/test_combine_first.py b/pandas/tests/series/methods/test_combine_first.py index 51d6704e1905b..233aae9ed1483 100644 --- a/pandas/tests/series/methods/test_combine_first.py +++ b/pandas/tests/series/methods/test_combine_first.py @@ -144,3 +144,12 @@ def test_combine_mixed_timezone(self): ), ) tm.assert_series_equal(result, expected) + + def test_combine_first_none_not_nan(self): + # GH#58977 + s1 = Series([None, None, None], index=["a", "b", "c"]) + s2 = Series([None, None, None], index=["b", "c", "d"]) + + result = s1.combine_first(s2) + expected = Series([None] * 4, index=["a", "b", "c", "d"]) + tm.assert_series_equal(result, expected)