Skip to content

Commit 1209160

Browse files
authored
REGR: None converted to NaN when enlarging Series (#48697)
* REGR: None cast to nan for object dtype when enlarging Series * Add gh ref * Add fixture
1 parent fc9b62a commit 1209160

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
@@ -2112,9 +2112,14 @@ def _setitem_with_indexer_missing(self, indexer, value):
21122112
# this preserves dtype of the value and of the object
21132113
if not is_scalar(value):
21142114
new_dtype = None
2115+
21152116
elif is_valid_na_for_dtype(value, self.obj.dtype):
2116-
value = na_value_for_dtype(self.obj.dtype, compat=False)
2117+
if not is_object_dtype(self.obj.dtype):
2118+
# Every NA value is suitable for object, no conversion needed
2119+
value = na_value_for_dtype(self.obj.dtype, compat=False)
2120+
21172121
new_dtype = maybe_promote(self.obj.dtype, value)[0]
2122+
21182123
elif isna(value):
21192124
new_dtype = None
21202125
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)