Skip to content

Commit e823c97

Browse files
committed
BUG: Inserting ndim=0 array does not infer string dtype
1 parent 6f0cd8d commit e823c97

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v2.1.2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Bug fixes
2424
~~~~~~~~~
2525
- Fixed bug in :meth:`DataFrame.resample` not respecting ``closed`` and ``label`` arguments for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55282`)
2626
- Fixed bug in :meth:`DataFrame.resample` where bin edges were not correct for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55281`)
27-
-
27+
- Fixed bug in :meth:`DataFrame.__setitem__` not inferring string dtype for zero-dimensional array with ``infer_string=True`` (:issue:`TODO`)
2828

2929
.. ---------------------------------------------------------------------------
3030
.. _whatsnew_212.other:

pandas/core/construction.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,14 @@ 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-
data = construct_1d_arraylike_from_scalar(data, len(index), dtype)
565+
if isinstance(data, str) and using_pyarrow_string_dtype():
566+
from pandas.core.arrays.string_ import StringDtype
567+
568+
dtype = StringDtype("pyarrow_numpy")
569+
data = dtype.construct_array_type()._from_sequence_of_strings([data])
570+
else:
571+
data = construct_1d_arraylike_from_scalar(data, len(index), dtype)
572+
566573
return data
567574

568575
elif isinstance(data, ABCExtensionArray):

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,18 @@ 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#
1910+
df = DataFrame({"x": [1]})
1911+
with pd.option_context("future.infer_string", True):
1912+
df.loc[df["x"] == 1, "y"] = "1"
1913+
expected = DataFrame(
1914+
{"x": [1], "y": Series(["1"], dtype="string[pyarrow_numpy]")},
1915+
columns=Index(["x", "y"], dtype="string[pyarrow_numpy]"),
1916+
)
1917+
tm.assert_frame_equal(df, expected)
1918+
1919+
19081920
class TestSetitemValidation:
19091921
# This is adapted from pandas/tests/arrays/masked/test_indexing.py
19101922
# but checks for warnings instead of errors.

0 commit comments

Comments
 (0)