Skip to content

Backport PR #51741 on branch 2.0.x (BUG: indexing empty pyarrow backed object returning corrupt object) #51841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ Indexing
- Bug in :meth:`DataFrame.compare` does not recognize differences when comparing ``NA`` with value in nullable dtypes (:issue:`48939`)
- Bug in :meth:`Series.rename` with :class:`MultiIndex` losing extension array dtypes (:issue:`21055`)
- Bug in :meth:`DataFrame.isetitem` coercing extension array dtypes in :class:`DataFrame` to object (:issue:`49922`)
- Bug in :meth:`Series.__getitem__` returning corrupt object when selecting from an empty pyarrow backed object (:issue:`51734`)
- Bug in :class:`BusinessHour` would cause creation of :class:`DatetimeIndex` to fail when no opening hour was included in the index (:issue:`49835`)

Missing
Expand Down
1 change: 1 addition & 0 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
else:
FLOAT_PYARROW_DTYPES_STR_REPR = []
ALL_INT_PYARROW_DTYPES_STR_REPR = []
ALL_PYARROW_DTYPES = []


EMPTY_STRING_PATTERN = re.compile("^$")
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,12 @@ def _concat_same_type(
ArrowExtensionArray
"""
chunks = [array for ea in to_concat for array in ea._data.iterchunks()]
arr = pa.chunked_array(chunks)
if to_concat[0].dtype == "string":
# StringDtype has no attrivute pyarrow_dtype
pa_dtype = pa.string()
else:
pa_dtype = to_concat[0].dtype.pyarrow_dtype
arr = pa.chunked_array(chunks, type=pa_dtype)
return cls(arr)

def _accumulate(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2298,3 +2298,11 @@ def test_dt_tz_localize(unit):
dtype=ArrowDtype(pa.timestamp(unit, "US/Pacific")),
)
tm.assert_series_equal(result, expected)


def test_concat_empty_arrow_backed_series(dtype):
# GH#51734
ser = pd.Series([], dtype=dtype)
expected = ser.copy()
result = pd.concat([ser[np.array([], dtype=np.bool_)]])
tm.assert_series_equal(result, expected)