Skip to content

Commit

Permalink
Backport PR #48697 on branch 1.5.x (REGR: None converted to NaN when …
Browse files Browse the repository at this point in the history
…enlarging Series) (#48745)

Backport PR #48697: REGR: None converted to NaN when enlarging Series

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl authored Sep 23, 2022
1 parent 4449a8a commit b28b5c4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
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
~~~~~~~~~~~~~~~~~
- 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 regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2108,9 +2108,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, 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
Expand Down

0 comments on commit b28b5c4

Please sign in to comment.