Skip to content

REGR: None converted to NaN when enlarging Series #48697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
~~~~~~~~~~~~~~~~~
- 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`)
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, 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