Skip to content
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

BUG: incompatible dtype when creating string column with loc #55248

Closed
Closed
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: 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
Loading