-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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(string dtype): Implement cumsum for Python-backed strings #60938
Merged
+92
−20
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
51b363e
ENH(string dtype): Implement cumsum for Python-backed strings
rhshadrach 188f92e
cleanups
rhshadrach c04969e
cleanups
rhshadrach 7e116ef
type-hint fixup
rhshadrach 0629fcf
More type fixes
rhshadrach d0f6673
Use quotes for cast
rhshadrach 7f9571d
Refinements
rhshadrach 2fd9779
type-ignore
rhshadrach 85093a6
Merge branch 'main' into enh_cumsum_for_np_str
rhshadrach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -49,6 +49,7 @@ | |||||
) | ||||||
|
||||||
from pandas.core import ( | ||||||
missing, | ||||||
nanops, | ||||||
ops, | ||||||
) | ||||||
|
@@ -870,6 +871,87 @@ def _reduce( | |||||
|
||||||
raise TypeError(f"Cannot perform reduction '{name}' with string dtype") | ||||||
|
||||||
def _accumulate(self, name: str, *, skipna: bool = True, **kwargs) -> StringArray: | ||||||
""" | ||||||
Return an ExtensionArray performing an accumulation operation. | ||||||
|
||||||
The underlying data type might change. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
name : str | ||||||
Name of the function, supported values are: | ||||||
- cummin | ||||||
- cummax | ||||||
- cumsum | ||||||
- cumprod | ||||||
skipna : bool, default True | ||||||
If True, skip NA values. | ||||||
**kwargs | ||||||
Additional keyword arguments passed to the accumulation function. | ||||||
Currently, there is no supported kwarg. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
array | ||||||
|
||||||
Raises | ||||||
------ | ||||||
NotImplementedError : subclass does not define accumulations | ||||||
""" | ||||||
if name == "cumprod": | ||||||
msg = f"operation '{name}' not supported for dtype '{self.dtype}'" | ||||||
raise TypeError(msg) | ||||||
|
||||||
# We may need to strip out trailing NA values | ||||||
tail: np.ndarray | None = None | ||||||
na_mask: np.ndarray | None = None | ||||||
ndarray = self._ndarray | ||||||
np_func = { | ||||||
"cumsum": np.cumsum, | ||||||
"cummin": np.minimum.accumulate, | ||||||
"cummax": np.maximum.accumulate, | ||||||
}[name] | ||||||
|
||||||
if self._hasna: | ||||||
na_mask = cast("npt.NDArray[np.bool_]", isna(ndarray)) | ||||||
if np.all(na_mask): | ||||||
return type(self)(ndarray) | ||||||
if skipna: | ||||||
if name == "cumsum": | ||||||
ndarray = np.where(na_mask, "", ndarray) | ||||||
else: | ||||||
# We can retain the running min/max by forward/backward filling. | ||||||
ndarray = ndarray.copy() | ||||||
missing.pad_or_backfill_inplace( | ||||||
ndarray.T, | ||||||
method="pad", | ||||||
axis=0, | ||||||
) | ||||||
missing.pad_or_backfill_inplace( | ||||||
ndarray.T, | ||||||
method="backfill", | ||||||
axis=0, | ||||||
) | ||||||
else: | ||||||
# When not skipping NA values, the result should be null from | ||||||
# the first NA value onward. | ||||||
idx = np.argmax(na_mask) | ||||||
tail = np.empty(len(ndarray) - idx, dtype="object") | ||||||
tail[:] = np.nan | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
So we directly fill it with the appropriate NA value (although I assume the constructor would fix it up anyway) |
||||||
ndarray = ndarray[:idx] | ||||||
|
||||||
# mypy: Cannot call function of unknown type | ||||||
np_result = np_func(ndarray) # type: ignore[operator] | ||||||
|
||||||
if tail is not None: | ||||||
np_result = np.hstack((np_result, tail)) | ||||||
elif na_mask is not None: | ||||||
np_result = np.where(na_mask, np.nan, np_result) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
result = type(self)(np_result) | ||||||
return result | ||||||
|
||||||
def _wrap_reduction_result(self, axis: AxisInt | None, result) -> Any: | ||||||
if self.dtype.na_value is np.nan and result is libmissing.NA: | ||||||
# the masked_reductions use pd.NA -> convert to np.nan | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
Is the
.T
needed? (I would think that ndarray is 1D)