Skip to content

BUG: indexing empty pyarrow backed object returning corrupt object #51741

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 10 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -1248,6 +1248,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 @@ -253,6 +253,7 @@
else:
FLOAT_PYARROW_DTYPES_STR_REPR = []
ALL_INT_PYARROW_DTYPES_STR_REPR = []
ALL_PYARROW_DTYPES = []


EMPTY_STRING_PATTERN = re.compile("^$")
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def __getitem__(self, item: PositionalIndexer):
pa_dtype = pa.string()
else:
pa_dtype = self._dtype.pyarrow_dtype
return type(self)(pa.chunked_array([], type=pa_dtype))
return type(self)(pa.array([], type=pa_dtype))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A chunked array without chunks should in theory also work, so this might point to something else that is buggy?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the error in #51734, it might be that the type needs to be specified in the chunked_array() call in _concat_same_type

Copy link
Member Author

@phofl phofl Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also solve this, but imo we should rather avoid returning something here that creates these problems. When iterating over a chunked array without chunks you’ll get an empty list, which makes determining the dtype tricky, because we would have to implement upcasting logic when getting more than one object

edit: forget what I’ve said about upcasting…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but imo we should rather avoid returning something here that creates these problems

Yes, but so my point is that we should maybe rather ensure that this does not create these problems, because there can be other ways that such a chunked array gets created (eg coming from pyarrow).
A chunkedarray itself also has a type object, so you don't need to get one chunk to get the type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I missed the concat_same_type, makes sense when we only have one type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

elif is_integer_dtype(item.dtype):
return self.take(item)
elif is_bool_dtype(item.dtype):
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 @@ -2156,3 +2156,11 @@ def test_boolean_reduce_series_all_null(all_boolean_reductions, skipna):
else:
expected = pd.NA
assert result is 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)