Skip to content

CLN: avoid values_from_object in nanops #32508

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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
)
from pandas.core.dtypes.missing import isna, na_value_for_dtype, notna

from pandas.core.construction import extract_array

bn = import_optional_dependency("bottleneck", raise_on_missing=False, on_version="warn")
_BOTTLENECK_INSTALLED = bn is not None
_USE_BOTTLENECK = False
Expand Down Expand Up @@ -284,14 +286,8 @@ def _get_values(

mask = _maybe_get_mask(values, skipna, mask)

if is_datetime64tz_dtype(values):
# lib.values_from_object returns M8[ns] dtype instead of tz-aware,
# so this case must be handled separately from the rest
dtype = values.dtype
values = getattr(values, "_values", values)
else:
values = lib.values_from_object(values)
dtype = values.dtype
values = extract_array(values, extract_numpy=True)
Copy link
Member

Choose a reason for hiding this comment

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

According to the typing and docstring, values is always an ndarray, so if those are correct, this should not be needed

Copy link
Member Author

Choose a reason for hiding this comment

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

so if those are correct

they are not.

dtype = values.dtype

if is_datetime_or_timedelta_dtype(values) or is_datetime64tz_dtype(values):
# changing timedelta64/datetime64 to int64 needs to happen after
Expand Down Expand Up @@ -758,7 +754,7 @@ def nanvar(values, axis=None, skipna=True, ddof=1, mask=None):
>>> nanops.nanvar(s)
1.0
"""
values = lib.values_from_object(values)
values = extract_array(values, extract_numpy=True)
dtype = values.dtype
mask = _maybe_get_mask(values, skipna, mask)
if is_any_int_dtype(values):
Expand Down Expand Up @@ -985,7 +981,7 @@ def nanskew(
>>> nanops.nanskew(s)
1.7320508075688787
"""
values = lib.values_from_object(values)
values = extract_array(values, extract_numpy=True)
mask = _maybe_get_mask(values, skipna, mask)
if not is_float_dtype(values.dtype):
values = values.astype("f8")
Expand Down Expand Up @@ -1069,7 +1065,7 @@ def nankurt(
>>> nanops.nankurt(s)
-1.2892561983471076
"""
values = lib.values_from_object(values)
values = extract_array(values, extract_numpy=True)
mask = _maybe_get_mask(values, skipna, mask)
if not is_float_dtype(values.dtype):
values = values.astype("f8")
Expand Down