Skip to content

BUG: iloc.setitem raising NotImplementedError for all null slice with one column df #47987

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 1 commit into from
Aug 8, 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,8 @@ Indexing
- Bug in setting a NA value (``None`` or ``np.nan``) into a :class:`Series` with int-based :class:`IntervalDtype` incorrectly casting to object dtype instead of a float-based :class:`IntervalDtype` (:issue:`45568`)
- Bug in indexing setting values into an ``ExtensionDtype`` column with ``df.iloc[:, i] = values`` with ``values`` having the same dtype as ``df.iloc[:, i]`` incorrectly inserting a new array instead of setting in-place (:issue:`33457`)
- Bug in :meth:`Series.__setitem__` with a non-integer :class:`Index` when using an integer key to set a value that cannot be set inplace where a ``ValueError`` was raised instead of casting to a common dtype (:issue:`45070`)
- Bug in :meth:`DataFrame.loc` raising ``NotImplementedError`` when setting value into one column :class:`DataFrame` with all null slice as column indexer (:issue:`45469`)
- Bug in :meth:`DataFrame.loc` not casting ``None`` to ``NA`` when setting value a list into :class:`DataFrame` (:issue:`47987`)
- Bug in :meth:`Series.__setitem__` when setting incompatible values into a ``PeriodDtype`` or ``IntervalDtype`` :class:`Series` raising when indexing with a boolean mask but coercing when indexing with otherwise-equivalent indexers; these now consistently coerce, along with :meth:`Series.mask` and :meth:`Series.where` (:issue:`45768`)
- Bug in :meth:`DataFrame.where` with multiple columns with datetime-like dtypes failing to downcast results consistent with other dtypes (:issue:`45837`)
- Bug in :func:`isin` upcasting to ``float64`` with unsigned integer dtype and list-like argument without a dtype (:issue:`46485`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ def __setitem__(self, key, value):
if len(value) and not lib.is_string_array(value, skipna=True):
raise ValueError("Must provide strings.")

value[isna(value)] = libmissing.NA

super().__setitem__(key, value)

def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,10 @@ def _unwrap_setitem_indexer(self, indexer):
elif lib.is_integer(indexer[1]) and indexer[1] == 0:
# reached via setitem_single_block passing the whole indexer
indexer = indexer[0]

elif com.is_null_slice(indexer[1]):
indexer = indexer[0]
Comment on lines 1719 to +1724
Copy link
Contributor

@weikhor weikhor Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small opinion. Is it better if we combine together condition like this?

elif (lib.is_integer(indexer[1]) and indexer[1] == 0) or (com.is_null_slice(indexer[1])):
     # reached via setitem_single_block passing the whole indexer
     indexer = indexer[0]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put it into two because this is easier to read imo


else:
raise NotImplementedError(
"This should not be reached. Please report a bug at "
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,22 @@ def test_loc_internals_not_updated_correctly(self):
)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("val", [None, [None], pd.NA, [pd.NA]])
def test_iloc_setitem_string_list_na(self, val):
# GH#45469
df = DataFrame({"a": ["a", "b", "c"]}, dtype="string")
df.iloc[[0], :] = val
expected = DataFrame({"a": [pd.NA, "b", "c"]}, dtype="string")
tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize("val", [None, pd.NA])
def test_iloc_setitem_string_na(self, val):
# GH#45469
df = DataFrame({"a": ["a", "b", "c"]}, dtype="string")
df.iloc[0, :] = val
expected = DataFrame({"a": [pd.NA, "b", "c"]}, dtype="string")
tm.assert_frame_equal(df, expected)


class TestDataFrameIndexingUInt64:
def test_setitem(self, uint64_frame):
Expand Down