Skip to content

Commit f4359ec

Browse files
Backport PR #55366 on branch 2.1.x (BUG: Inserting ndim=0 array does not infer string dtype) (#55396)
Backport PR #55366: BUG: Inserting ndim=0 array does not infer string dtype Co-authored-by: Patrick Hoefler <[email protected]>
1 parent ad5fe9c commit f4359ec

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
Bug fixes
2323
~~~~~~~~~
2424
- Fixed bug in :meth:`Categorical.equals` if other has arrow backed string dtype (:issue:`55364`)
25+
- Fixed bug in :meth:`DataFrame.__setitem__` not inferring string dtype for zero-dimensional array with ``infer_string=True`` (:issue:`55366`)
2526
- Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`)
2627
- Fixed bug in :meth:`Index.insert` raising when inserting ``None`` into :class:`Index` with ``dtype="string[pyarrow_numpy]"`` (:issue:`55365`)
2728
- Silence ``Period[B]`` warnings introduced by :issue:`53446` during normal plotting activity (:issue:`55138`)

pandas/core/construction.py

+5
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,12 @@ def sanitize_array(
562562
if not is_list_like(data):
563563
if index is None:
564564
raise ValueError("index must be specified when data is not list-like")
565+
if isinstance(data, str) and using_pyarrow_string_dtype():
566+
from pandas.core.arrays.string_ import StringDtype
567+
568+
dtype = StringDtype("pyarrow_numpy")
565569
data = construct_1d_arraylike_from_scalar(data, len(index), dtype)
570+
566571
return data
567572

568573
elif isinstance(data, ABCExtensionArray):

pandas/tests/frame/indexing/test_indexing.py

+13
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,19 @@ def test_adding_new_conditional_column() -> None:
19051905
tm.assert_frame_equal(df, expected)
19061906

19071907

1908+
def test_add_new_column_infer_string():
1909+
# GH#55366
1910+
pytest.importorskip("pyarrow")
1911+
df = DataFrame({"x": [1]})
1912+
with pd.option_context("future.infer_string", True):
1913+
df.loc[df["x"] == 1, "y"] = "1"
1914+
expected = DataFrame(
1915+
{"x": [1], "y": Series(["1"], dtype="string[pyarrow_numpy]")},
1916+
columns=Index(["x", "y"], dtype="string[pyarrow_numpy]"),
1917+
)
1918+
tm.assert_frame_equal(df, expected)
1919+
1920+
19081921
class TestSetitemValidation:
19091922
# This is adapted from pandas/tests/arrays/masked/test_indexing.py
19101923
# but checks for warnings instead of errors.

0 commit comments

Comments
 (0)