Skip to content

Commit a9efc05

Browse files
committed
Merge branch 'indexing_ndim_0' into string_dtype_tests
# Conflicts: # doc/source/whatsnew/v2.1.2.rst
2 parents 3cf79ef + e823c97 commit a9efc05

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v2.1.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Bug fixes
2525
- Fixed bug in :meth:`DataFrame.interpolate` raising incorrect error message (:issue:`55347`)
2626
- Fixed bug in :meth:`DataFrame.resample` not respecting ``closed`` and ``label`` arguments for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55282`)
2727
- Fixed bug in :meth:`DataFrame.resample` where bin edges were not correct for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55281`)
28+
- Fixed bug in :meth:`DataFrame.__setitem__` not inferring string dtype for zero-dimensional array with ``infer_string=True`` (:issue:`TODO`)
2829
- Fixed bug in :meth:`Categorical.equals` if other has arrow backed string dtype (:issue:`TODO`)
2930
- Fixed bug in :meth:`Index.insert` raising when inserting ``None`` into :class:`Index` with ``dtype="string[pyarrow_numpy]"`` (issue:`TODO`)
3031
- Fixed bug in :meth:`Series.rank` for ``string[pyarrow_numpy]`` dtype (:issue:`TODO`)

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
@@ -1918,6 +1918,18 @@ def test_adding_new_conditional_column() -> None:
19181918
tm.assert_frame_equal(df, expected)
19191919

19201920

1921+
def test_add_new_column_infer_string():
1922+
# GH#
1923+
df = DataFrame({"x": [1]})
1924+
with pd.option_context("future.infer_string", True):
1925+
df.loc[df["x"] == 1, "y"] = "1"
1926+
expected = DataFrame(
1927+
{"x": [1], "y": Series(["1"], dtype="string[pyarrow_numpy]")},
1928+
columns=Index(["x", "y"], dtype="string[pyarrow_numpy]"),
1929+
)
1930+
tm.assert_frame_equal(df, expected)
1931+
1932+
19211933
class TestSetitemValidation:
19221934
# This is adapted from pandas/tests/arrays/masked/test_indexing.py
19231935
# but checks for warnings instead of errors.

0 commit comments

Comments
 (0)