Skip to content

Commit b28b5c4

Browse files
Backport PR #48697 on branch 1.5.x (REGR: None converted to NaN when enlarging Series) (#48745)
Backport PR #48697: REGR: None converted to NaN when enlarging Series Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 4449a8a commit b28b5c4

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17+
- Fixed Regression in :meth:`Series.__setitem__` casting ``None`` to ``NaN`` for object dtype (:issue:`48665`)
1718
- Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`)
1819
- Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`)
1920
- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)

pandas/core/indexing.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2108,9 +2108,14 @@ def _setitem_with_indexer_missing(self, indexer, value):
21082108
# this preserves dtype of the value and of the object
21092109
if not is_scalar(value):
21102110
new_dtype = None
2111+
21112112
elif is_valid_na_for_dtype(value, self.obj.dtype):
2112-
value = na_value_for_dtype(self.obj.dtype, compat=False)
2113+
if not is_object_dtype(self.obj.dtype):
2114+
# Every NA value is suitable for object, no conversion needed
2115+
value = na_value_for_dtype(self.obj.dtype, compat=False)
2116+
21132117
new_dtype = maybe_promote(self.obj.dtype, value)[0]
2118+
21142119
elif isna(value):
21152120
new_dtype = None
21162121
elif not self.obj.empty and not is_object_dtype(self.obj.dtype):

pandas/tests/series/indexing/test_setitem.py

+8
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,14 @@ def test_setitem_enlarge_with_na(self, na, target_na, dtype, target_dtype, index
562562
expected = Series(expected_values, dtype=target_dtype)
563563
tm.assert_series_equal(ser, expected)
564564

565+
def test_setitem_enlargement_object_none(self, nulls_fixture):
566+
# GH#48665
567+
ser = Series(["a", "b"])
568+
ser[3] = nulls_fixture
569+
expected = Series(["a", "b", nulls_fixture], index=[0, 1, 3])
570+
tm.assert_series_equal(ser, expected)
571+
assert ser[3] is nulls_fixture
572+
565573

566574
def test_setitem_scalar_into_readonly_backing_data():
567575
# GH#14359: test that you cannot mutate a read only buffer

0 commit comments

Comments
 (0)