Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
- Regression in :meth:`Series.__setitem__` casting ``None`` to ``NaN`` for object dtype (:issue:`48665`)
- Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`)
-

Expand Down
7 changes: 6 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
# GH#48665
ser = Series(["a", "b"])
ser[3] = None
expected = Series(["a", "b", None], index=[0, 1, 3])
tm.assert_series_equal(ser, expected)
assert ser[3] is None


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