Skip to content
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

ENH: Implement cum* methods for PyArrow strings #60633

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

rhshadrach
Copy link
Member

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

Implements cumsum, cummin, and cummax for PyArrow-backed strings. I don't see a way to do this without passing through NumPy - if there are other ideas for approaching happy to give those a shot.

cc @WillAyd @jorisvandenbossche

@rhshadrach rhshadrach added Enhancement Strings String extension data type and string data Arrow pyarrow functionality Transformations e.g. cumsum, diff, rank labels Dec 31, 2024
@rhshadrach rhshadrach added this to the 2.3 milestone Dec 31, 2024
@WillAyd
Copy link
Member

WillAyd commented Dec 31, 2024

I don't see a way to do this without passing through NumPy - if there are other ideas for approaching happy to give those a shot.

The only other way I think that would be reasonable to implement this in would be to use nanoarrow, but that's a larger implementation. I think this is fine for now - just not very performant but that can always be improved later

@rhshadrach rhshadrach requested a review from WillAyd January 4, 2025 15:36
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

lgtm. thanks for getting this started

probably worth adding a note for 3.0 as well

@@ -1317,6 +1317,22 @@ def nullable_string_dtype(request):
return request.param


@pytest.fixture(
params=[
pytest.param("str[pyarrow]", marks=td.skip_if_no("pyarrow")),
Copy link
Member

Choose a reason for hiding this comment

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

I was going to comment: I don't think this can work. Although it is then strange the tests are passing :) But it seems this was not doing what I think you expected it was doing -> #60661

I would use the same approach of creating the dtype through StringDtype(..) like in some of the fixtures above

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks!

nulls = pc.is_null(pa_array)
idx = pc.index(nulls, True).as_py()
tail = pa.nulls(len(pa_array) - idx, type=pa_array.type)
pa_array = pa_array[:idx].combine_chunks()
Copy link
Member

Choose a reason for hiding this comment

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

Is the combine chunks needed here? (I would expect that the conversion to numpy (when calling the numpy func) will do this automatically (and potentially more efficiently))

pandas/tests/apply/test_str.py Show resolved Hide resolved
(["x", "z", "y"], "cumsum", False, ["x", "xz", "xzy"]),
(["x", pd.NA, "y"], "cumsum", True, ["x", "x", "xy"]),
(["x", pd.NA, "y"], "cumsum", False, ["x", pd.NA, pd.NA]),
([pd.NA, "x", "y"], "cumsum", True, ["", "x", "xy"]),
Copy link
Member

Choose a reason for hiding this comment

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

It seems that for numerical data, we actually (somewhat inconsistently?) propagate leading NAs:

In [7]: pd.Series([np.nan, 0.5, 2.5]).cumsum()
Out[7]: 
0    NaN
1    0.5
2    3.0
dtype: float64

(i.e. the result doesn't have 0.0 for the first element)

Copy link
Member

Choose a reason for hiding this comment

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

Actually not related to "leading" NAs. It seems what is happening is that missing values are ignored to calculate the cumulative result, but then are propagated to the result elementwise. This is also shown in the docstring example of cumsum, so this seems intentional.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for catching this. Agreed we should match this behavior. I do find it odd, but that's (possibly) for another day!

@rhshadrach rhshadrach marked this pull request as draft January 5, 2025 17:46
@rhshadrach rhshadrach marked this pull request as ready for review January 5, 2025 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality Enhancement Strings String extension data type and string data Transformations e.g. cumsum, diff, rank
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants