Skip to content

Commit

Permalink
dont raise unnecessary warning
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Sep 22, 2023
1 parent 2a08b05 commit 0ee58c7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed bug where PDEP-6 warning about setting an item of an incompatible dtype was being shown when creating a new conditional column (:issue:`55025`)
-

.. ---------------------------------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
ABCSeries,
)
from pandas.core.dtypes.missing import (
infer_fill_value,
is_valid_na_for_dtype,
isna,
na_value_for_dtype,
Expand Down Expand Up @@ -1841,8 +1840,12 @@ def _setitem_with_indexer(self, indexer, value, name: str = "iloc"):
self.obj[key] = empty_value

else:
# FIXME: GH#42099#issuecomment-864326014
self.obj[key] = infer_fill_value(value)
# avoid circular import
from pandas.core.series import Series

new = Series(index=self.obj.index)
new[key] = value
self.obj[key] = new

new_indexer = convert_from_missing_indexer_tuple(
indexer, self.obj.axes
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,14 @@ def test_setitem_dict_and_set_disallowed_multiindex(self, key):
df.loc[key] = 1


def test_adding_new_conditional_column() -> None:
# https://github.com/pandas-dev/pandas/issues/55025
df = DataFrame({"x": [1]})
df.loc[df["x"] == 1, "y"] = "1"
expected = DataFrame({"x": [1], "y": ["1"]})
tm.assert_frame_equal(df, expected)


class TestSetitemValidation:
# This is adapted from pandas/tests/arrays/masked/test_indexing.py
# but checks for warnings instead of errors.
Expand Down

0 comments on commit 0ee58c7

Please sign in to comment.