Skip to content

CLN: rename reduce-->do_reduce #27706

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 17 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ cdef class BlockSlider:
arr.shape[1] = 0


def reduce(arr, f, axis=0, dummy=None, labels=None):
def do_reduce(arr, f, axis=0, dummy=None, labels=None):
Copy link
Contributor

@jreback jreback Aug 2, 2019

Choose a reason for hiding this comment

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

I don't particular like this name, we don't have anywhere we call things do_*, how about
compute_reduction / apply_reduce

Copy link
Member Author

Choose a reason for hiding this comment

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

sure

Copy link
Member Author

Choose a reason for hiding this comment

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

(and its such an easy change to make because I can grep for it!)

"""

Parameters
Expand Down
3 changes: 2 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.do_reduce(self.values, self.f, axis=self.axis)
except Exception:
result = np.apply_along_axis(self.f, self.axis, self.values)

Expand Down Expand Up @@ -281,7 +281,7 @@ def apply_standard(self):
dummy = Series(empty_arr, index=index, dtype=values.dtype)

try:
result = reduction.reduce(
result = reduction.do_reduce(
values, self.f, axis=self.axis, dummy=dummy, labels=labels
)
return self.obj._constructor_sliced(result, index=labels)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down
15 changes: 2 additions & 13 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -224,8 +213,8 @@ class TimelikeOps:

.. versionadded:: 0.24.0

nonexistent : 'shift_forward', 'shift_backward', 'NaT', timedelta, \
default 'raise'
nonexistent : 'shift_forward', 'shift_backward', 'NaT', timedelta,
default 'raise'
Copy link
Member

Choose a reason for hiding this comment

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

this is for proper rendering with numpydoc

Copy link
Member Author

Choose a reason for hiding this comment

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

darn, will revert.

A nonexistent time does not exist in a particular timezone
where clocks moved forward due to DST.

Expand Down
1 change: 0 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ class DatetimeArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps, dtl.DatelikeOps
# -----------------------------------------------------------------
# Constructors

_attributes = ["freq", "tz"]
Copy link
Member

Choose a reason for hiding this comment

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

Why are you removing those attributes? (I suppose they are thus not used) It's a left-over from when it shared code with the Index?

Copy link
Member Author

Choose a reason for hiding this comment

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

exactly, leftover from the inheritance days

_dtype = None # type: Union[np.dtype, DatetimeTZDtype]
_freq = None

Expand Down
1 change: 0 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3563,7 +3563,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.

Expand Down Expand Up @@ -6190,8 +6190,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:
Expand Down Expand Up @@ -6254,7 +6252,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))
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -343,7 +345,7 @@ class _GroupBy(PandasObject, SelectionMixin):

def __init__(
self,
obj,
obj: NDFrame,
keys=None,
axis=0,
level=None,
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -423,7 +424,7 @@ def groups(self):


def _get_grouper(
obj,
obj: NDFrame,
key=None,
axis=0,
level=None,
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCDatetimeArray,
ABCDatetimeIndex,
ABCIndex,
ABCIndexClass,
ABCSeries,
ABCSparseArray,
ABCSparseSeries,
ABCTimedeltaArray,
ABCTimedeltaIndex,
)
from pandas.core.dtypes.missing import isna, notna

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 (
Expand Down Expand Up @@ -1015,7 +1017,7 @@ def wrapper(left, right):
right = np.broadcast_to(right, left.shape)
right = pd.TimedeltaIndex(right)

assert isinstance(right, (pd.TimedeltaIndex, ABCTimedeltaArray, ABCSeries))
assert isinstance(right, (ABCTimedeltaIndex, ABCTimedeltaArray, ABCSeries))
try:
result = op(left._values, right)
except NullFrequencyError:
Expand All @@ -1033,7 +1035,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)

Expand Down Expand Up @@ -1205,7 +1207,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"
)
Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/groupby/test_bin_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,29 @@ 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.do_reduce(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.do_reduce(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.do_reduce(
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.do_reduce(
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.do_reduce(
arr, np.sum, axis=1, dummy=dummy, labels=Index(np.arange(100))
)
assert_almost_equal(result, expected)