-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
base: main
Are you sure you want to change the base?
ENH: Implement cum* methods for PyArrow strings #60633
Conversation
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 |
There was a problem hiding this 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
pandas/conftest.py
Outdated
@@ -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")), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
pandas/core/arrays/arrow/array.py
Outdated
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() |
There was a problem hiding this comment.
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))
(["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"]), |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.Implements
cumsum
,cummin
, andcummax
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