From 1cb17b02e1dee87790ed93b71ab7a18bca219cec Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 5 Nov 2019 19:06:52 -0800 Subject: [PATCH 1/5] REF: separate out ShallowMixin --- pandas/core/base.py | 34 +++++++++++++++++++--------------- pandas/core/resample.py | 4 ++-- pandas/core/window/rolling.py | 19 +++++++++++++++++-- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 1a2f906f97152..65e531f96614a 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -4,7 +4,7 @@ import builtins from collections import OrderedDict import textwrap -from typing import Dict, FrozenSet, Optional +from typing import Dict, FrozenSet, List, Optional import warnings import numpy as np @@ -569,7 +569,7 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis): try: new_res = colg.aggregate(a) - except (TypeError, DataError): + except TypeError: pass else: results.append(new_res) @@ -618,6 +618,23 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis): raise ValueError("cannot combine transform and aggregation operations") return result + def _get_cython_func(self, arg: str) -> Optional[str]: + """ + if we define an internal function for this argument, return it + """ + return self._cython_table.get(arg) + + def _is_builtin_func(self, arg): + """ + if we define an builtin function for this argument, return it, + otherwise return the arg + """ + return self._builtin_table.get(arg, arg) + + +class ShallowMixin: + _attributes = [] # type: List[str] + def _shallow_copy(self, obj=None, obj_type=None, **kwargs): """ return a new object with the replacement attributes @@ -633,19 +650,6 @@ def _shallow_copy(self, obj=None, obj_type=None, **kwargs): kwargs[attr] = getattr(self, attr) return obj_type(obj, **kwargs) - def _get_cython_func(self, arg: str) -> Optional[str]: - """ - if we define an internal function for this argument, return it - """ - return self._cython_table.get(arg) - - def _is_builtin_func(self, arg): - """ - if we define an builtin function for this argument, return it, - otherwise return the arg - """ - return self._builtin_table.get(arg, arg) - class IndexOpsMixin: """ diff --git a/pandas/core/resample.py b/pandas/core/resample.py index e68a2efc3f4e6..9d7ddcf3c7727 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -17,7 +17,7 @@ from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries import pandas.core.algorithms as algos -from pandas.core.base import DataError +from pandas.core.base import DataError, ShallowMixin from pandas.core.generic import _shared_docs from pandas.core.groupby.base import GroupByMixin from pandas.core.groupby.generic import SeriesGroupBy @@ -34,7 +34,7 @@ _shared_docs_kwargs = dict() # type: Dict[str, str] -class Resampler(_GroupBy): +class Resampler(_GroupBy, ShallowMixin): """ Class for resampling datetimelike data, a groupby-like operation. See aggregate, transform, and apply functions on this object. diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 68eb1f630bfc3..58673c744589d 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -35,7 +35,7 @@ ) from pandas._typing import Axis, FrameOrSeries, Scalar -from pandas.core.base import DataError, PandasObject, SelectionMixin +from pandas.core.base import DataError, PandasObject, SelectionMixin, ShallowMixin import pandas.core.common as com from pandas.core.index import Index, ensure_index from pandas.core.window.common import ( @@ -50,7 +50,7 @@ ) -class _Window(PandasObject, SelectionMixin): +class _Window(PandasObject, ShallowMixin, SelectionMixin): _attributes = [ "window", "min_periods", @@ -91,6 +91,21 @@ def __init__( def _constructor(self): return Window + def _shallow_copy(self, obj=None, obj_type=None, **kwargs): + """ + return a new object with the replacement attributes + """ + if obj is None: + obj = self._selected_obj.copy() + if obj_type is None: + obj_type = self._constructor + if isinstance(obj, obj_type): + obj = obj.obj + for attr in self._attributes: + if attr not in kwargs: + kwargs[attr] = getattr(self, attr) + return obj_type(obj, **kwargs) + @property def is_datetimelike(self) -> Optional[bool]: return None From d0baad45e84150acc39021c3a4b0ea0c3fd5bfa8 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 5 Nov 2019 19:11:02 -0800 Subject: [PATCH 2/5] remove duplicate --- pandas/core/window/rolling.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 58673c744589d..0718acd6360bf 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -91,21 +91,6 @@ def __init__( def _constructor(self): return Window - def _shallow_copy(self, obj=None, obj_type=None, **kwargs): - """ - return a new object with the replacement attributes - """ - if obj is None: - obj = self._selected_obj.copy() - if obj_type is None: - obj_type = self._constructor - if isinstance(obj, obj_type): - obj = obj.obj - for attr in self._attributes: - if attr not in kwargs: - kwargs[attr] = getattr(self, attr) - return obj_type(obj, **kwargs) - @property def is_datetimelike(self) -> Optional[bool]: return None From 3093e3df742b95c35bfd1af828f781e010cb203a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 6 Nov 2019 07:46:07 -0800 Subject: [PATCH 3/5] troublehsoot ci --- pandas/core/arrays/sparse/array.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index e1691de234335..da2fd2d2ce3ec 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -1581,6 +1581,9 @@ def make_sparse(arr, kind="block", fill_value=None, dtype=None, copy=False): indices = mask.nonzero()[0].astype(np.int32) index = _make_index(length, indices, kind) + + assert isinstance(mask, np.ndarray), type(mask) + assert mask.dtype == bool, mask.dtype sparsified_values = arr[mask] if dtype is not None: sparsified_values = astype_nansafe(sparsified_values, dtype=dtype) From 4bc35e2b46f070d072cfbd202041c07bf7c0a193 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 6 Nov 2019 08:10:41 -0800 Subject: [PATCH 4/5] troubleshoot npdev --- pandas/core/arrays/sparse/array.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index da2fd2d2ce3ec..45b5f05cece71 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -1582,8 +1582,10 @@ def make_sparse(arr, kind="block", fill_value=None, dtype=None, copy=False): index = _make_index(length, indices, kind) - assert isinstance(mask, np.ndarray), type(mask) - assert mask.dtype == bool, mask.dtype + if mask.dtype == object: + # numpy 1.18 requires that we do this explicitly + mask = mask.astype(bool) + sparsified_values = arr[mask] if dtype is not None: sparsified_values = astype_nansafe(sparsified_values, dtype=dtype) From 10bc57d27c72e1a2b4cdff195a6df770a9a16a68 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 6 Nov 2019 10:08:25 -0800 Subject: [PATCH 5/5] revert --- pandas/core/arrays/sparse/array.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 45b5f05cece71..e1691de234335 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -1581,11 +1581,6 @@ def make_sparse(arr, kind="block", fill_value=None, dtype=None, copy=False): indices = mask.nonzero()[0].astype(np.int32) index = _make_index(length, indices, kind) - - if mask.dtype == object: - # numpy 1.18 requires that we do this explicitly - mask = mask.astype(bool) - sparsified_values = arr[mask] if dtype is not None: sparsified_values = astype_nansafe(sparsified_values, dtype=dtype)