Skip to content
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

TYP: make some internal funcs keyword-only #37688

Merged
merged 1 commit into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
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
17 changes: 12 additions & 5 deletions pandas/core/array_algos/masked_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def _sumprod(
func: Callable,
values: np.ndarray,
mask: np.ndarray,
*,
skipna: bool = True,
min_count: int = 0,
):
Expand Down Expand Up @@ -52,19 +53,25 @@ def _sumprod(
return func(values, where=~mask)


def sum(values: np.ndarray, mask: np.ndarray, skipna: bool = True, min_count: int = 0):
def sum(
values: np.ndarray, mask: np.ndarray, *, skipna: bool = True, min_count: int = 0
):
return _sumprod(
np.sum, values=values, mask=mask, skipna=skipna, min_count=min_count
)


def prod(values: np.ndarray, mask: np.ndarray, skipna: bool = True, min_count: int = 0):
def prod(
values: np.ndarray, mask: np.ndarray, *, skipna: bool = True, min_count: int = 0
):
return _sumprod(
np.prod, values=values, mask=mask, skipna=skipna, min_count=min_count
)


def _minmax(func: Callable, values: np.ndarray, mask: np.ndarray, skipna: bool = True):
def _minmax(
func: Callable, values: np.ndarray, mask: np.ndarray, *, skipna: bool = True
):
"""
Reduction for 1D masked array.

Expand Down Expand Up @@ -94,9 +101,9 @@ def _minmax(func: Callable, values: np.ndarray, mask: np.ndarray, skipna: bool =
return libmissing.NA


def min(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
def min(values: np.ndarray, mask: np.ndarray, *, skipna: bool = True):
return _minmax(np.min, values=values, mask=mask, skipna=skipna)


def max(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
def max(values: np.ndarray, mask: np.ndarray, *, skipna: bool = True):
return _minmax(np.max, values=values, mask=mask, skipna=skipna)
3 changes: 2 additions & 1 deletion pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _validate_scalar(self, value):
def take(
self: _T,
indices: Sequence[int],
*,
allow_fill: bool = False,
fill_value: Any = None,
axis: int = 0,
Expand Down Expand Up @@ -246,7 +247,7 @@ def fillna(self: _T, value=None, method=None, limit=None) -> _T:
# ------------------------------------------------------------------------
# Reductions

def _reduce(self, name: str, skipna: bool = True, **kwargs):
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
meth = getattr(self, name, None)
if meth:
return meth(skipna=skipna, **kwargs)
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class ExtensionArray:
# ------------------------------------------------------------------------

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy=False):
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
"""
Construct a new ExtensionArray from a sequence of scalars.

Expand All @@ -195,7 +195,7 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
raise AbstractMethodError(cls)

@classmethod
def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):
def _from_sequence_of_strings(cls, strings, *, dtype=None, copy=False):
"""
Construct a new ExtensionArray from a sequence of strings.

Expand Down Expand Up @@ -922,7 +922,11 @@ def repeat(self, repeats, axis=None):
# ------------------------------------------------------------------------

def take(
self, indices: Sequence[int], allow_fill: bool = False, fill_value: Any = None
self,
indices: Sequence[int],
*,
allow_fill: bool = False,
fill_value: Any = None,
) -> "ExtensionArray":
"""
Take elements from an array.
Expand Down Expand Up @@ -1153,7 +1157,7 @@ def _concat_same_type(
# of objects
_can_hold_na = True

def _reduce(self, name: str, skipna: bool = True, **kwargs):
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
"""
Return a scalar result of performing the reduction operation.

Expand Down
12 changes: 7 additions & 5 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,17 @@ def dtype(self) -> BooleanDtype:
return self._dtype

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "BooleanArray":
def _from_sequence(
cls, scalars, *, dtype=None, copy: bool = False
) -> "BooleanArray":
if dtype:
assert dtype == "boolean"
values, mask = coerce_to_array(scalars, copy=copy)
return BooleanArray(values, mask)

@classmethod
def _from_sequence_of_strings(
cls, strings: List[str], dtype=None, copy: bool = False
cls, strings: List[str], *, dtype=None, copy: bool = False
) -> "BooleanArray":
def map_string(s):
if isna(s):
Expand All @@ -294,7 +296,7 @@ def map_string(s):
raise ValueError(f"{s} cannot be cast to bool")

scalars = [map_string(x) for x in strings]
return cls._from_sequence(scalars, dtype, copy)
return cls._from_sequence(scalars, dtype=dtype, copy=copy)

_HANDLED_TYPES = (np.ndarray, numbers.Number, bool, np.bool_)

Expand Down Expand Up @@ -682,12 +684,12 @@ def _arith_method(self, other, op):

return self._maybe_mask_result(result, mask, other, op_name)

def _reduce(self, name: str, skipna: bool = True, **kwargs):
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):

if name in {"any", "all"}:
return getattr(self, name)(skipna=skipna, **kwargs)

return super()._reduce(name, skipna, **kwargs)
return super()._reduce(name, skipna=skipna, **kwargs)

def _maybe_mask_result(self, result, mask, other, op_name: str):
"""
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 @@ -385,7 +385,7 @@ def _constructor(self) -> Type["Categorical"]:
return Categorical

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy=False):
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
return Categorical(scalars, dtype=dtype)

def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def _simple_new(
return result

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy: bool = False):
def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False):
return cls._from_sequence_not_strict(scalars, dtype=dtype, copy=copy)

@classmethod
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/arrays/floating.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,18 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
super().__init__(values, mask, copy=copy)

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "FloatingArray":
def _from_sequence(
cls, scalars, *, dtype=None, copy: bool = False
) -> "FloatingArray":
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
return FloatingArray(values, mask)

@classmethod
def _from_sequence_of_strings(
cls, strings, dtype=None, copy: bool = False
cls, strings, *, dtype=None, copy: bool = False
) -> "FloatingArray":
scalars = to_numeric(strings, errors="raise")
return cls._from_sequence(scalars, dtype, copy)
return cls._from_sequence(scalars, dtype=dtype, copy=copy)

_HANDLED_TYPES = (np.ndarray, numbers.Number)

Expand Down
8 changes: 5 additions & 3 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,17 @@ def __abs__(self):
return type(self)(np.abs(self._data), self._mask)

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "IntegerArray":
def _from_sequence(
cls, scalars, *, dtype=None, copy: bool = False
) -> "IntegerArray":
return integer_array(scalars, dtype=dtype, copy=copy)

@classmethod
def _from_sequence_of_strings(
cls, strings, dtype=None, copy: bool = False
cls, strings, *, dtype=None, copy: bool = False
) -> "IntegerArray":
scalars = to_numeric(strings, errors="raise")
return cls._from_sequence(scalars, dtype, copy)
return cls._from_sequence(scalars, dtype=dtype, copy=copy)

_HANDLED_TYPES = (np.ndarray, numbers.Number)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def _simple_new(cls, data, closed="right"):
return result

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy=False):
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
return cls(scalars, dtype=dtype, copy=copy)

@classmethod
Expand Down Expand Up @@ -788,7 +788,7 @@ def shift(self, periods: int = 1, fill_value: object = None) -> "IntervalArray":
b = empty
return self._concat_same_type([a, b])

def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):
def take(self, indices, *, allow_fill=False, fill_value=None, axis=None, **kwargs):
"""
Take elements from the IntervalArray.

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def _concat_same_type(cls: Type[BaseMaskedArrayT], to_concat) -> BaseMaskedArray
def take(
self: BaseMaskedArrayT,
indexer,
*,
allow_fill: bool = False,
fill_value: Optional[Scalar] = None,
) -> BaseMaskedArrayT:
Expand Down Expand Up @@ -357,7 +358,7 @@ def value_counts(self, dropna: bool = True) -> "Series":

return Series(counts, index=index)

def _reduce(self, name: str, skipna: bool = True, **kwargs):
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
data = self._data
mask = self._mask

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ def __init__(self, values: Union[np.ndarray, "PandasArray"], copy: bool = False)
self._dtype = PandasDtype(values.dtype)

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "PandasArray":
def _from_sequence(
cls, scalars, *, dtype=None, copy: bool = False
) -> "PandasArray":
if isinstance(dtype, PandasDtype):
dtype = dtype._dtype

Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def _simple_new(
def _from_sequence(
cls: Type["PeriodArray"],
scalars: Union[Sequence[Optional[Period]], AnyArrayLike],
*,
dtype: Optional[PeriodDtype] = None,
copy: bool = False,
) -> "PeriodArray":
Expand All @@ -214,9 +215,9 @@ def _from_sequence(

@classmethod
def _from_sequence_of_strings(
cls, strings, dtype=None, copy=False
cls, strings, *, dtype=None, copy=False
) -> "PeriodArray":
return cls._from_sequence(strings, dtype, copy)
return cls._from_sequence(strings, dtype=dtype, copy=copy)

@classmethod
def _from_datetime64(cls, data, freq, tz=None) -> "PeriodArray":
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def __setitem__(self, key, value):
raise TypeError(msg)

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy=False):
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
return cls(scalars, dtype=dtype)

@classmethod
Expand Down Expand Up @@ -809,7 +809,7 @@ def _get_val_at(self, loc):
val = maybe_box_datetimelike(val, self.sp_values.dtype)
return val

def take(self, indices, allow_fill=False, fill_value=None) -> "SparseArray":
def take(self, indices, *, allow_fill=False, fill_value=None) -> "SparseArray":
if is_scalar(indices):
raise ValueError(f"'indices' must be an array, not a scalar '{indices}'.")
indices = np.asarray(indices, dtype=np.int32)
Expand Down Expand Up @@ -1156,7 +1156,7 @@ def nonzero(self):
# Reductions
# ------------------------------------------------------------------------

def _reduce(self, name: str, skipna: bool = True, **kwargs):
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
method = getattr(self, name, None)

if method is None:
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def _validate(self):
)

@classmethod
def _from_sequence(cls, scalars, dtype=None, copy=False):
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
if dtype:
assert dtype == "string"

Expand Down Expand Up @@ -226,7 +226,7 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
return new_string_array

@classmethod
def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):
def _from_sequence_of_strings(cls, strings, *, dtype=None, copy=False):
return cls._from_sequence(strings, dtype=dtype, copy=copy)

def __arrow_array__(self, type=None):
Expand Down Expand Up @@ -295,7 +295,7 @@ def astype(self, dtype, copy=True):

return super().astype(dtype, copy)

def _reduce(self, name: str, skipna: bool = True, **kwargs):
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
if name in ["min", "max"]:
return getattr(self, name)(skipna=skipna)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def _simple_new(

@classmethod
def _from_sequence(
cls, data, dtype=TD64NS_DTYPE, copy: bool = False
cls, data, *, dtype=TD64NS_DTYPE, copy: bool = False
) -> "TimedeltaArray":
if dtype:
_validate_td64_dtype(dtype)
Expand Down
1 change: 1 addition & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ def _reduce(
self,
op,
name: str,
*,
axis=0,
skipna=True,
numeric_only=None,
Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8719,6 +8719,7 @@ def _reduce(
self,
op,
name: str,
*,
axis=0,
skipna=True,
numeric_only=None,
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4190,7 +4190,15 @@ def f(x):
)

def _reduce(
self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds
self,
op,
name: str,
*,
axis=0,
skipna=True,
numeric_only=None,
filter_type=None,
**kwds,
):
"""
Perform a reduction operation.
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/arrow/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _concat_same_type(cls, to_concat):
def __invert__(self):
return type(self).from_scalars(~self._data.to_pandas())

def _reduce(self, name: str, skipna: bool = True, **kwargs):
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
if skipna:
arr = self[~self.isna()]
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _formatter(self, boxed=False):
def _concat_same_type(cls, to_concat):
return cls(np.concatenate([x._data for x in to_concat]))

def _reduce(self, name: str, skipna: bool = True, **kwargs):
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):

if skipna:
# If we don't have any NAs, we can ignore skipna
Expand Down