Skip to content

Commit 6b84daa

Browse files
authored
BUG: Inserting ndim=0 array does not infer string dtype (#55366)
1 parent 1c1bb85 commit 6b84daa

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
@@ -23,6 +23,7 @@ Fixed regressions
2323
Bug fixes
2424
~~~~~~~~~
2525
- Fixed bug in :meth:`Categorical.equals` if other has arrow backed string dtype (:issue:`55364`)
26+
- Fixed bug in :meth:`DataFrame.__setitem__` not inferring string dtype for zero-dimensional array with ``infer_string=True`` (:issue:`55366`)
2627
- Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`)
2728
- Fixed bug in :meth:`DataFrame.interpolate` raising incorrect error message (:issue:`55347`)
2829
- Fixed bug in :meth:`DataFrame.resample` not respecting ``closed`` and ``label`` arguments for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55282`)

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)