-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: put all post-processing at end of DataFrame._reduce #32671
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
Changes from all commits
Commits
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 hidden or 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 |
---|---|---|
|
@@ -7808,6 +7808,8 @@ def _reduce( | |
self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds | ||
): | ||
|
||
assert filter_type is None or filter_type == "bool", filter_type | ||
|
||
dtype_is_dt = self.dtypes.apply( | ||
lambda x: is_datetime64_any_dtype(x) or is_period_dtype(x) | ||
) | ||
|
@@ -7835,7 +7837,7 @@ def f(x): | |
return op(x, axis=axis, skipna=skipna, **kwds) | ||
|
||
def _get_data(axis_matters): | ||
if filter_type is None or filter_type == "numeric": | ||
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. would it be clearer to have "numeric" as the default value of filter_type instead of None. Again maybe a docstring would help. |
||
if filter_type is None: | ||
data = self._get_numeric_data() | ||
elif filter_type == "bool": | ||
if axis_matters: | ||
|
@@ -7882,15 +7884,11 @@ def blk_func(values): | |
return out | ||
|
||
if numeric_only is None: | ||
values = self.values | ||
data = self | ||
values = data.values | ||
try: | ||
result = f(values) | ||
|
||
if filter_type == "bool" and is_object_dtype(values) and axis is None: | ||
# work around https://github.com/numpy/numpy/issues/10489 | ||
# TODO: combine with hasattr(result, 'dtype') further down | ||
# hard since we don't have `values` down there. | ||
result = np.bool_(result) | ||
except TypeError: | ||
# e.g. in nanops trying to convert strs to float | ||
|
||
|
@@ -7916,30 +7914,36 @@ def blk_func(values): | |
|
||
# TODO: why doesnt axis matter here? | ||
data = _get_data(axis_matters=False) | ||
with np.errstate(all="ignore"): | ||
result = f(data.values) | ||
labels = data._get_agg_axis(axis) | ||
|
||
values = data.values | ||
with np.errstate(all="ignore"): | ||
result = f(values) | ||
else: | ||
if numeric_only: | ||
data = _get_data(axis_matters=True) | ||
labels = data._get_agg_axis(axis) | ||
|
||
values = data.values | ||
labels = data._get_agg_axis(axis) | ||
else: | ||
values = self.values | ||
data = self | ||
values = data.values | ||
result = f(values) | ||
|
||
if hasattr(result, "dtype") and is_object_dtype(result.dtype): | ||
if filter_type == "bool" and is_object_dtype(values) and axis is None: | ||
# work around https://github.com/numpy/numpy/issues/10489 | ||
# TODO: can we de-duplicate parts of this with the next blocK? | ||
result = np.bool_(result) | ||
elif hasattr(result, "dtype") and is_object_dtype(result.dtype): | ||
try: | ||
if filter_type is None or filter_type == "numeric": | ||
if filter_type is None: | ||
result = result.astype(np.float64) | ||
elif filter_type == "bool" and notna(result).all(): | ||
result = result.astype(np.bool_) | ||
except (ValueError, TypeError): | ||
|
||
# try to coerce to the original dtypes item by item if we can | ||
if axis == 0: | ||
result = coerce_to_dtypes(result, self.dtypes) | ||
result = coerce_to_dtypes(result, data.dtypes) | ||
|
||
if constructor is not None: | ||
result = self._constructor_sliced(result, index=labels) | ||
|
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.
does it make sense to add type annotation to filter_type as well? and maybe start a docstring.
also the NotImplementedError in _get_data is now unreachable?
also
out_dtype = "bool" if filter_type == "bool" else None
on L7864 is redundant?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.
filter_type is only passed for "any" and "all", id lean towards getting rid of the kwarg altogether
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.
that has always been the case, but im happy to get rid of that branch altogether