Skip to content

bugfix for #48757 #48876

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

Closed
wants to merge 1 commit into from
Closed
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
37 changes: 19 additions & 18 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,20 +692,20 @@ def nanmean(
values, mask, dtype, dtype_max, _ = _get_values(
values, skipna, fill_value=0, mask=mask
)
dtype_sum = dtype_max
# dtype_sum = dtype_max
dtype_count = np.dtype(np.float64)

# not using needs_i8_conversion because that includes period
if dtype.kind in ["m", "M"]:
dtype_sum = np.dtype(np.float64)
elif is_integer_dtype(dtype):
dtype_sum = np.dtype(np.float64)
elif is_float_dtype(dtype):
dtype_sum = dtype
dtype_count = dtype
# if dtype.kind in ["m", "M"]:
# dtype_sum = np.dtype(np.float64)
# elif is_integer_dtype(dtype):
# dtype_sum = np.dtype(np.float64)
# elif is_float_dtype(dtype):
# dtype_sum = dtype
# dtype_count = dtype

count = _get_counts(values.shape, mask, axis, dtype=dtype_count)
the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum))
the_sum = _ensure_numeric(values.sum(axis, dtype=np.float64))
Copy link
Member

Choose a reason for hiding this comment

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

We don't want to cast in integer situations

Copy link
Author

Choose a reason for hiding this comment

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

Oh, Do would like to complete this pull or fix this bug? I have no idea how to fit it in a right way. Thank you for your reply!


if axis is not None and getattr(the_sum, "ndim", False):
count = cast(np.ndarray, count)
Expand Down Expand Up @@ -911,9 +911,10 @@ def nanstd(

orig_dtype = values.dtype
values, mask, _, _, _ = _get_values(values, skipna, mask=mask)

result = np.sqrt(nanvar(values, axis=axis, skipna=skipna, ddof=ddof, mask=mask))
return _wrap_results(result, orig_dtype)
# return _wrap_results(result, np.orig_dtype)
return result



@disallow("M8", "m8")
Expand Down Expand Up @@ -961,10 +962,10 @@ def nanvar(
if mask is not None:
values[mask] = np.nan

if is_float_dtype(values.dtype):
count, d = _get_counts_nanvar(values.shape, mask, axis, ddof, values.dtype)
else:
count, d = _get_counts_nanvar(values.shape, mask, axis, ddof)
# if is_float_dtype(values.dtype):
# count, d = _get_counts_nanvar(values.shape, mask, axis, ddof, values.dtype)
# else:
count, d = _get_counts_nanvar(values.shape, mask, axis, ddof)

if skipna and mask is not None:
values = values.copy()
Expand All @@ -979,16 +980,16 @@ def nanvar(
avg = _ensure_numeric(values.sum(axis=axis, dtype=np.float64)) / count
if axis is not None:
avg = np.expand_dims(avg, axis)
sqr = _ensure_numeric((avg - values) ** 2)
sqr = _ensure_numeric((np.subtract(avg , values,dtype=np.float64)) ** 2)
if mask is not None:
np.putmask(sqr, mask, 0)
result = sqr.sum(axis=axis, dtype=np.float64) / d

# Return variance as np.float64 (the datatype used in the accumulator),
# unless we were dealing with a float array, in which case use the same
# precision as the original values array.
if is_float_dtype(dtype):
result = result.astype(dtype, copy=False)
# if is_float_dtype(dtype):
# result = result.astype(dtype, copy=False)
return result


Expand Down