Skip to content

Commit

Permalink
Backport PR #55366 on branch 2.1.x (BUG: Inserting ndim=0 array does …
Browse files Browse the repository at this point in the history
…not infer string dtype) (#55396)

Backport PR #55366: BUG: Inserting ndim=0 array does not infer string dtype

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Oct 4, 2023
1 parent ad5fe9c commit f4359ec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
- Fixed bug in :meth:`Categorical.equals` if other has arrow backed string dtype (:issue:`55364`)
- Fixed bug in :meth:`DataFrame.__setitem__` not inferring string dtype for zero-dimensional array with ``infer_string=True`` (:issue:`55366`)
- Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`)
- Fixed bug in :meth:`Index.insert` raising when inserting ``None`` into :class:`Index` with ``dtype="string[pyarrow_numpy]"`` (:issue:`55365`)
- Silence ``Period[B]`` warnings introduced by :issue:`53446` during normal plotting activity (:issue:`55138`)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,12 @@ def sanitize_array(
if not is_list_like(data):
if index is None:
raise ValueError("index must be specified when data is not list-like")
if isinstance(data, str) and using_pyarrow_string_dtype():
from pandas.core.arrays.string_ import StringDtype

dtype = StringDtype("pyarrow_numpy")
data = construct_1d_arraylike_from_scalar(data, len(index), dtype)

return data

elif isinstance(data, ABCExtensionArray):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,19 @@ def test_adding_new_conditional_column() -> None:
tm.assert_frame_equal(df, expected)


def test_add_new_column_infer_string():
# GH#55366
pytest.importorskip("pyarrow")
df = DataFrame({"x": [1]})
with pd.option_context("future.infer_string", True):
df.loc[df["x"] == 1, "y"] = "1"
expected = DataFrame(
{"x": [1], "y": Series(["1"], dtype="string[pyarrow_numpy]")},
columns=Index(["x", "y"], dtype="string[pyarrow_numpy]"),
)
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

0 comments on commit f4359ec

Please sign in to comment.