diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 3f4265245bcb6..c998b2ce99628 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -14,6 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ +- Fixed Regression in :meth:`Series.__setitem__` casting ``None`` to ``NaN`` for object dtype (:issue:`48665`) - Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`) - Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`) - Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index bca8b3d459245..591ab9630ebb5 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2112,9 +2112,14 @@ def _setitem_with_indexer_missing(self, indexer, value): # this preserves dtype of the value and of the object if not is_scalar(value): new_dtype = None + elif is_valid_na_for_dtype(value, self.obj.dtype): - value = na_value_for_dtype(self.obj.dtype, compat=False) + if not is_object_dtype(self.obj.dtype): + # Every NA value is suitable for object, no conversion needed + value = na_value_for_dtype(self.obj.dtype, compat=False) + new_dtype = maybe_promote(self.obj.dtype, value)[0] + elif isna(value): new_dtype = None elif not self.obj.empty and not is_object_dtype(self.obj.dtype): diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 90051405c6935..9ab3b6ead017f 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -562,6 +562,14 @@ def test_setitem_enlarge_with_na(self, na, target_na, dtype, target_dtype, index expected = Series(expected_values, dtype=target_dtype) tm.assert_series_equal(ser, expected) + def test_setitem_enlargement_object_none(self, nulls_fixture): + # GH#48665 + ser = Series(["a", "b"]) + ser[3] = nulls_fixture + expected = Series(["a", "b", nulls_fixture], index=[0, 1, 3]) + tm.assert_series_equal(ser, expected) + assert ser[3] is nulls_fixture + def test_setitem_scalar_into_readonly_backing_data(): # GH#14359: test that you cannot mutate a read only buffer