8000 REF: implement _wrap_reduction_result by jbrockmendel · Pull Request #37660 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

REF: implement _wrap_reduction_result #37660

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 18 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
checkpoint tests passing
  • Loading branch information
jbrockmendel committed Oct 27, 2020
commit bdad92ce718542b01b0fc7a93f8a1afd53fed210
34 changes: 14 additions & 20 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,18 +1266,15 @@ def min(self, axis=None, skipna=True, *args, **kwargs):
nv.validate_min(args, kwargs)
nv.validate_minmax_axis(axis, self.ndim)

# View as M8[ns] to get correct NaT/masking semantics for PeriodDtype
result = nanops.nanmin(
self._ndarray.view("M8[ns]"), skipna=skipna, mask=self.isna()
)
if is_period_dtype(self.dtype):
result = self.to_timestamp("S").min(axis=axis, skipna=skipna)
if result is not NaT:
result = result.to_period(self.freq)
return result

result = nanops.nanmin(self._ndarray, skipna=skipna, mask=self.isna())
if lib.is_scalar(result):
if isna(result):
# Period._from_ordinal does not handle NaT gracefully
return NaT
# nanops may unwantedly cast to Timestamp
result = getattr(result, "value", result)
return self._box_func(result)
result = result.astype("i8", copy=False)
return self._from_backing_data(result)

def max(self, axis=None, skipna=True, *args, **kwargs):
Expand All @@ -1296,18 +1293,15 @@ def max(self, axis=None, skipna=True, *args, **kwargs):
nv.validate_max(args, kwargs)
nv.validate_minmax_axis(axis, self.ndim)

# View as M8[ns] to get correct NaT/masking semantics for PeriodDtype
result = nanops.nanmax(
self._ndarray.view("M8[ns]"), skipna=skipna, mask=self.isna()
)
if is_period_dtype(self.dtype):
result = self.to_timestamp("S").max(axis=axis, skipna=skipna)
if result is not NaT:
result = result.to_period(self.freq)
return result

result = nanops.nanmax(self._ndarray, skipna=skipna, mask=self.isna())
if lib.is_scalar(result):
if isna(result):
# Period._from_ordinal does not handle NaT gracefully
return NaT
# nanops may unwantedly cast to Timestamp
result = getattr(result, "value", result)
return self._box_func(result)
result = result.astype("i8", copy=False)
return self._from_backing_data(result)

def mean(self, skipna=True):
Expand Down
1 change: 1 addition & 0 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ def _wrap_results(result, dtype: DtypeObj, fill_value=None):
if result == fill_value:
result = np.nan
if tz is not None:
# we get here e.g. via nanmean when we call it on a DTA[tz]
result = Timestamp(result, tz=tz)
elif isna(result):
result = np.datetime64("NaT", "ns")
Expand Down
0