diff --git a/pandas/_libs/reduction.pyx b/pandas/_libs/reduction.pyx index 739ac0ed397ca..f95685c337969 100644 --- a/pandas/_libs/reduction.pyx +++ b/pandas/_libs/reduction.pyx @@ -628,7 +628,7 @@ cdef class BlockSlider: arr.shape[1] = 0 -def reduce(arr, f, axis=0, dummy=None, labels=None): +def compute_reduction(arr, f, axis=0, dummy=None, labels=None): """ Parameters diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 6a32553fe2d38..d24aafae0967d 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1280,7 +1280,8 @@ class Timedelta(_Timedelta): else: raise ValueError( "Value must be Timedelta, string, integer, " - "float, timedelta or convertible") + "float, timedelta or convertible, not {typ}" + .format(typ=type(value).__name__)) if is_timedelta64_object(value): value = value.view('i8') diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 2246bbfde636d..5c8599dbb054b 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -221,7 +221,7 @@ def apply_raw(self): """ apply to the values as a numpy array """ try: - result = reduction.reduce(self.values, self.f, axis=self.axis) + result = reduction.compute_reduction(self.values, self.f, axis=self.axis) except Exception: result = np.apply_along_axis(self.f, self.axis, self.values) @@ -281,7 +281,7 @@ def apply_standard(self): dummy = Series(empty_arr, index=index, dtype=values.dtype) try: - result = reduction.reduce( + result = reduction.compute_reduction( values, self.f, axis=self.axis, dummy=dummy, labels=labels ) return self.obj._constructor_sliced(result, index=labels) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index b16217d5d0a32..d22b4bd4d3f2b 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2703,7 +2703,7 @@ def _convert_to_list_like(list_like): elif is_scalar(list_like): return [list_like] else: - # is this reached? + # TODO: is this reached? return [list_like] diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 47718fc39ca1d..9b516c1b6ae02 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -57,21 +57,10 @@ class AttributesMixin: _data = None # type: np.ndarray - @property - def _attributes(self): - # Inheriting subclass should implement _attributes as a list of strings - raise AbstractMethodError(self) - @classmethod def _simple_new(cls, values, **kwargs): raise AbstractMethodError(cls) - def _get_attributes_dict(self): - """ - return an attributes dict for my class - """ - return {k: getattr(self, k, None) for k in self._attributes} - @property def _scalar_type(self) -> Type[DatetimeLikeScalar]: """The scalar associated with this datelike diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 6a4ca0ab4147a..061ee4b90d0e9 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -328,7 +328,6 @@ class DatetimeArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps, dtl.DatelikeOps # ----------------------------------------------------------------- # Constructors - _attributes = ["freq", "tz"] _dtype = None # type: Union[np.dtype, DatetimeTZDtype] _freq = None diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 6203cfdf6df6b..20ce11c70c344 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -161,7 +161,6 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, dtl.DatelikeOps): # array priority higher than numpy scalars __array_priority__ = 1000 - _attributes = ["freq"] _typ = "periodarray" # ABCPeriodArray _scalar_type = Period diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index dd0b9a79c6dca..afd1e8203059e 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -199,7 +199,6 @@ def dtype(self): # ---------------------------------------------------------------- # Constructors - _attributes = ["freq"] def __init__(self, values, dtype=_TD_DTYPE, freq=None, copy=False): if isinstance(values, (ABCSeries, ABCIndexClass)): diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ea49a13439bfb..010e5b890c5b5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3556,7 +3556,7 @@ def _iget_item_cache(self, item): def _box_item_values(self, key, values): raise AbstractMethodError(self) - def _slice(self, slobj, axis=0, kind=None): + def _slice(self, slobj: slice, axis=0, kind=None): """ Construct a slice of this container. @@ -6183,8 +6183,6 @@ def fillna( axis = 0 axis = self._get_axis_number(axis) - from pandas import DataFrame - if value is None: if self._is_mixed_type and axis == 1: @@ -6247,7 +6245,7 @@ def fillna( new_data = self._data.fillna( value=value, limit=limit, inplace=inplace, downcast=downcast ) - elif isinstance(value, DataFrame) and self.ndim == 2: + elif isinstance(value, ABCDataFrame) and self.ndim == 2: new_data = self.where(self.notna(), value) else: raise ValueError("invalid fill value with a %s" % type(value)) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index ec526b338eee1..c5e81e21e9fd5 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -29,14 +29,16 @@ class providing the base-class of operations. from pandas.core.dtypes.cast import maybe_downcast_to_dtype from pandas.core.dtypes.common import ( ensure_float, + is_datetime64_dtype, is_datetime64tz_dtype, is_extension_array_dtype, + is_integer_dtype, is_numeric_dtype, + is_object_dtype, is_scalar, ) from pandas.core.dtypes.missing import isna, notna -from pandas.api.types import is_datetime64_dtype, is_integer_dtype, is_object_dtype import pandas.core.algorithms as algorithms from pandas.core.arrays import Categorical from pandas.core.base import ( @@ -343,7 +345,7 @@ class _GroupBy(PandasObject, SelectionMixin): def __init__( self, - obj, + obj: NDFrame, keys=None, axis=0, level=None, @@ -360,8 +362,8 @@ def __init__( self._selection = selection - if isinstance(obj, NDFrame): - obj._consolidate_inplace() + assert isinstance(obj, NDFrame), type(obj) + obj._consolidate_inplace() self.level = level diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 5c32550af3883..143755a47b97b 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -25,6 +25,7 @@ from pandas.core.arrays import Categorical, ExtensionArray import pandas.core.common as com from pandas.core.frame import DataFrame +from pandas.core.generic import NDFrame from pandas.core.groupby.categorical import recode_for_groupby, recode_from_groupby from pandas.core.groupby.ops import BaseGrouper from pandas.core.index import CategoricalIndex, Index, MultiIndex @@ -423,7 +424,7 @@ def groups(self): def _get_grouper( - obj, + obj: NDFrame, key=None, axis=0, level=None, diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 1484feeeada64..f20c3f702e29d 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -906,7 +906,7 @@ def _get_sorted_data(self): return self.data.take(self.sort_idx, axis=self.axis) def _chop(self, sdata, slice_obj): - return sdata.iloc[slice_obj] + raise AbstractMethodError(self) def apply(self, f): raise AbstractMethodError(self) @@ -933,7 +933,7 @@ def _chop(self, sdata, slice_obj): if self.axis == 0: return sdata.iloc[slice_obj] else: - return sdata._slice(slice_obj, axis=1) # .loc[:, slice_obj] + return sdata._slice(slice_obj, axis=1) def get_splitter(data, *args, **kwargs): diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 01bfbed1aab4c..2c8006680626c 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -37,6 +37,7 @@ from pandas.core.dtypes.generic import ( ABCDataFrame, ABCDatetimeArray, + ABCDatetimeIndex, ABCIndex, ABCIndexClass, ABCSeries, @@ -47,7 +48,7 @@ import pandas as pd from pandas._typing import ArrayLike -import pandas.core.common as com +from pandas.core.construction import extract_array from . import missing from .docstrings import ( @@ -1022,7 +1023,7 @@ def wrapper(left, right): # does inference in the case where `result` has object-dtype. return construct_result(left, result, index=left.index, name=res_name) - elif isinstance(right, (ABCDatetimeArray, pd.DatetimeIndex)): + elif isinstance(right, (ABCDatetimeArray, ABCDatetimeIndex)): result = op(left._values, right) return construct_result(left, result, index=left.index, name=res_name) @@ -1194,7 +1195,7 @@ def wrapper(self, other, axis=None): ) # always return a full value series here - res_values = com.values_from_object(res) + res_values = extract_array(res, extract_numpy=True) return self._constructor( res_values, index=self.index, name=res_name, dtype="bool" ) diff --git a/pandas/tests/groupby/test_bin_groupby.py b/pandas/tests/groupby/test_bin_groupby.py index b240876de92b1..2195686ee9c7f 100644 --- a/pandas/tests/groupby/test_bin_groupby.py +++ b/pandas/tests/groupby/test_bin_groupby.py @@ -6,15 +6,13 @@ from pandas.core.dtypes.common import ensure_int64 -from pandas import Index, isna +from pandas import Index, Series, isna from pandas.core.groupby.ops import generate_bins_generic import pandas.util.testing as tm from pandas.util.testing import assert_almost_equal def test_series_grouper(): - from pandas import Series - obj = Series(np.random.randn(10)) dummy = obj[:0] @@ -31,8 +29,6 @@ def test_series_grouper(): def test_series_bin_grouper(): - from pandas import Series - obj = Series(np.random.randn(10)) dummy = obj[:0] @@ -123,30 +119,32 @@ class TestMoments: class TestReducer: def test_int_index(self): - from pandas.core.series import Series - arr = np.random.randn(100, 4) - result = reduction.reduce(arr, np.sum, labels=Index(np.arange(4))) + result = reduction.compute_reduction(arr, np.sum, labels=Index(np.arange(4))) expected = arr.sum(0) assert_almost_equal(result, expected) - result = reduction.reduce(arr, np.sum, axis=1, labels=Index(np.arange(100))) + result = reduction.compute_reduction( + arr, np.sum, axis=1, labels=Index(np.arange(100)) + ) expected = arr.sum(1) assert_almost_equal(result, expected) dummy = Series(0.0, index=np.arange(100)) - result = reduction.reduce(arr, np.sum, dummy=dummy, labels=Index(np.arange(4))) + result = reduction.compute_reduction( + arr, np.sum, dummy=dummy, labels=Index(np.arange(4)) + ) expected = arr.sum(0) assert_almost_equal(result, expected) dummy = Series(0.0, index=np.arange(4)) - result = reduction.reduce( + result = reduction.compute_reduction( arr, np.sum, axis=1, dummy=dummy, labels=Index(np.arange(100)) ) expected = arr.sum(1) assert_almost_equal(result, expected) - result = reduction.reduce( + result = reduction.compute_reduction( arr, np.sum, axis=1, dummy=dummy, labels=Index(np.arange(100)) ) assert_almost_equal(result, expected)