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

Conversation

phofl
Copy link
Member

@phofl phofl commented Mar 2, 2023

looks like an empty chunked array creates problems later on.

@phofl phofl added this to the 2.0 milestone Mar 2, 2023
@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Arrow pyarrow functionality labels Mar 2, 2023
@phofl phofl requested a review from mroeschke March 2, 2023 10:19
@@ -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

@j-bennet
Copy link

j-bennet commented Mar 3, 2023

I can confirm that it solves the original issue.

@@ -1012,7 +1012,11 @@ 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":
Copy link
Member

Choose a reason for hiding this comment

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

Is needed specifically for StringDtype("pyarrow") and not ArrowDtype(pa.string())?

If so, could you add a comment to that effect?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes since the StringDtype does not have a pyarrow_dtype

Copy link
Member Author

Choose a reason for hiding this comment

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

Added

phofl added 2 commits March 7, 2023 22:28
# Conflicts:
#	pandas/tests/extension/test_arrow.py
@phofl phofl merged commit a07cb65 into pandas-dev:main Mar 8, 2023
@lumberbot-app
Copy link

lumberbot-app bot commented Mar 8, 2023

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 2.0.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 a07cb65459f24446e82354854ff6658f29414c0a
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #51741: BUG: indexing empty pyarrow backed object returning corrupt object'
  1. Push to a named branch:
git push YOURFORK 2.0.x:auto-backport-of-pr-51741-on-2.0.x
  1. Create a PR against branch 2.0.x, I would have named this PR:

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

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: pd.concat fails with GroupBy.head() and pd.StringDtype["pyarrow"]
4 participants