Skip to content

ENH: Implement more str accessor methods for ArrowDtype #52614

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 12 commits into from
Apr 21, 2023

Conversation

mroeschke
Copy link
Member

Most of these don't have corresponding pyarrow compute methods

@mroeschke mroeschke added Strings String extension data type and string data Arrow pyarrow functionality labels Apr 12, 2023
@mroeschke mroeschke modified the milestones: 2.1, 2.0.1 Apr 12, 2023
None if val.as_py() is None else val.as_py().partition(sep)
for val in chunk
]
for chunk in self._pa_array.iterchunks()
Copy link
Member

Choose a reason for hiding this comment

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

it looks like this will maintain the chunking structure. is there a reason not to chain these together and end up with a single chunk?

Copy link
Member Author

Choose a reason for hiding this comment

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

I saw this related issue about ops not maintaining the underlying chunking structure and though best to try to keep it here: #42357

for val in chunk
]
for chunk in self._pa_array.iterchunks()
]
Copy link
Member

Choose a reason for hiding this comment

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

is it worth making a helper for this pattern so this can just be

def _str_index(...)
    predicate = lambda x: x.index(sub, start, end)
    return self._helper_whatever(predicate)

ser = pd.Series(["abc", None], dtype=ArrowDtype(pa.string()))
with pytest.raises(
NotImplementedError, match=f"str.{method} not supported with pd.ArrowDtype"
NotImplementedError, match="str.extract not supported with pd.ArrowDtype"
Copy link
Member

Choose a reason for hiding this comment

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

out of curiosity why is this one left out?

Copy link
Member Author

Choose a reason for hiding this comment

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

str.extract was just a lot trickier to implement. I can try revisiting it in a followup

@jorisvandenbossche
Copy link
Member

In general, it's often best to avoid iterating over pyarrow arrays (wrapping each array element in a pyarrow Scalar, and then converting each individually to a python object with as_py() has quite some overhead). Typically, first converting the whole array to numpy and iterating over the numpy array can be more efficient:

import string, random

In [23]: arr = pa.array(["".join(random.choices(string.ascii_letters, k=5)) for _ in range(1_000_000)])

In [24]: %timeit [val.as_py() for val in arr]
517 ms ± 10.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [25]: %timeit [val for val in arr.to_numpy(zero_copy_only=False)]
80.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Wrapping in and converting the scalar has quite some overhead, while the conversion to numpy is better optimized and happens in one go at a lower level.

@mroeschke
Copy link
Member Author

Going to merge this in, I can address any followups if needed.

@mroeschke mroeschke merged commit fbcbdaf into pandas-dev:main Apr 21, 2023
@mroeschke mroeschke deleted the enh/str/more_arrow_string branch April 21, 2023 23:41
@lumberbot-app
Copy link

lumberbot-app bot commented Apr 21, 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 fbcbdaf70da354801d99a1cebb889153a3eca481
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #52614: ENH: Implement more str accessor methods for ArrowDtype'
  1. Push to a named branch:
git push YOURFORK 2.0.x:auto-backport-of-pr-52614-on-2.0.x
  1. Create a PR against branch 2.0.x, I would have named this PR:

"Backport PR #52614 on branch 2.0.x (ENH: Implement more str accessor methods for ArrowDtype)"

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.

mroeschke added a commit to mroeschke/pandas that referenced this pull request Apr 21, 2023
phofl pushed a commit that referenced this pull request Apr 22, 2023
Rylie-W pushed a commit to Rylie-W/pandas that referenced this pull request May 19, 2023
…2614)

* Add more str arrow functions

* Finish functions

* finish methods and add tests

* Finish implementing

* Fix >3.8 compat

* Create helper function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants