Skip to content

Commit

Permalink
BUG: incompatible dtype when creating string column with loc (#55249)
Browse files Browse the repository at this point in the history
dont raise unnecessary warning
  • Loading branch information
MarcoGorelli authored Sep 22, 2023
1 parent cae3f16 commit f19f81f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 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
3 changes: 3 additions & 0 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ def infer_fill_value(val):
return np.array("NaT", dtype=DT64NS_DTYPE)
elif dtype in ["timedelta", "timedelta64"]:
return np.array("NaT", dtype=TD64NS_DTYPE)
return np.array(np.nan, dtype=object)
elif val.dtype.kind == "U":
return np.array(np.nan, dtype=val.dtype)
return np.nan


Expand Down
15 changes: 15 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,21 @@ 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)

df = DataFrame({"x": [1]})
# try inserting something which numpy would store as 'object'
value = lambda x: x
df.loc[df["x"] == 1, "y"] = value
expected = DataFrame({"x": [1], "y": [value]})
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
5 changes: 1 addition & 4 deletions pandas/tests/frame/indexing/test_set_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ def test_set_value_resize(self, float_frame):
assert float_frame._get_value("foobar", "qux") == 0

res = float_frame.copy()
with tm.assert_produces_warning(
FutureWarning, match="Setting an item of incompatible dtype"
):
res._set_value("foobar", "baz", "sam")
res._set_value("foobar", "baz", "sam")
assert res["baz"].dtype == np.object_

res = float_frame.copy()
Expand Down

0 comments on commit f19f81f

Please sign in to comment.