Skip to content

CLN: remove unneeded try/except in nanops #52684

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 3 commits into from
Apr 17, 2023

Conversation

topper-123
Copy link
Contributor

@topper-123 topper-123 commented Apr 15, 2023

Look like this try/except isn't needed, checking in CI if it verifies the result.

except (AttributeError, TypeError, ValueError):
result = np.nan
result = getattr(values, meth)(axis, dtype=dtype_max)
result.fill(np.nan)
Copy link
Member

Choose a reason for hiding this comment

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

do we have tests that get here?

@topper-123
Copy link
Contributor Author

I rearranged it again. if (axis is not None and values.shape[axis] == 0) or values.size == 0: is always the same as if values.size == 0: AFAIKT.

So I've just added a if values.size == 0 clause at the top and cleaned up even further, so it's quite simple now.

@mroeschke mroeschke added this to the 2.1 milestone Apr 17, 2023
@@ -1067,22 +1067,14 @@ def reduction(
axis: AxisInt | None = None,
skipna: bool = True,
mask: npt.NDArray[np.bool_] | None = None,
) -> Dtype:
Copy link
Member

Choose a reason for hiding this comment

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

Can we keep this type annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This annotation was wrong and this doesn't return a Dtype but the the result of a min/max of a DataFrame/Series, i.e. typically a scalar or a ndarray (but the min/max of a Series can in principle be anything....)

IDK if we return type Scalar as annotation for Series?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah not either, but since this is an inner function the typing here probably doesn't matter as much

dtype = values.dtype
):
if values.size == 0:
return _na_for_min_count(values, axis)
Copy link
Member

Choose a reason for hiding this comment

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

I assume we have tests that get here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The values.size == 0 case is actually never reached in the current code:

You can see this function is wrapped in bottleneck_switch and if you go to bottleneck_switch you can see (line 125) it takes care of the values.size == 0 case, also when use_bottleneck = False (kwds.get("min_count") is None is always true for _nanminmax).

I just think this is more readable than the old code.

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha. Thanks for the explanation

@mroeschke mroeschke merged commit 832bb05 into pandas-dev:main Apr 17, 2023
@mroeschke
Copy link
Member

Thanks @topper-123

@topper-123 topper-123 deleted the cln_nanminmax branch April 17, 2023 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants