Skip to content

Commit ea706b3

Browse files
authored
TYP: make more internal funcs keyword-only (#37688)
1 parent 188258b commit ea706b3

20 files changed

+71
-39
lines changed

pandas/core/array_algos/masked_reductions.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def _sumprod(
1717
func: Callable,
1818
values: np.ndarray,
1919
mask: np.ndarray,
20+
*,
2021
skipna: bool = True,
2122
min_count: int = 0,
2223
):
@@ -52,19 +53,25 @@ def _sumprod(
5253
return func(values, where=~mask)
5354

5455

55-
def sum(values: np.ndarray, mask: np.ndarray, skipna: bool = True, min_count: int = 0):
56+
def sum(
57+
values: np.ndarray, mask: np.ndarray, *, skipna: bool = True, min_count: int = 0
58+
):
5659
return _sumprod(
5760
np.sum, values=values, mask=mask, skipna=skipna, min_count=min_count
5861
)
5962

6063

61-
def prod(values: np.ndarray, mask: np.ndarray, skipna: bool = True, min_count: int = 0):
64+
def prod(
65+
values: np.ndarray, mask: np.ndarray, *, skipna: bool = True, min_count: int = 0
66+
):
6267
return _sumprod(
6368
np.prod, values=values, mask=mask, skipna=skipna, min_count=min_count
6469
)
6570

6671

67-
def _minmax(func: Callable, values: np.ndarray, mask: np.ndarray, skipna: bool = True):
72+
def _minmax(
73+
func: Callable, values: np.ndarray, mask: np.ndarray, *, skipna: bool = True
74+
):
6875
"""
6976
Reduction for 1D masked array.
7077
@@ -94,9 +101,9 @@ def _minmax(func: Callable, values: np.ndarray, mask: np.ndarray, skipna: bool =
94101
return libmissing.NA
95102

96103

97-
def min(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
104+
def min(values: np.ndarray, mask: np.ndarray, *, skipna: bool = True):
98105
return _minmax(np.min, values=values, mask=mask, skipna=skipna)
99106

100107

101-
def max(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
108+
def max(values: np.ndarray, mask: np.ndarray, *, skipna: bool = True):
102109
return _minmax(np.max, values=values, mask=mask, skipna=skipna)

pandas/core/arrays/_mixins.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def _validate_scalar(self, value):
5454
def take(
5555
self: _T,
5656
indices: Sequence[int],
57+
*,
5758
allow_fill: bool = False,
5859
fill_value: Any = None,
5960
axis: int = 0,
@@ -246,7 +247,7 @@ def fillna(self: _T, value=None, method=None, limit=None) -> _T:
246247
# ------------------------------------------------------------------------
247248
# Reductions
248249

249-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
250+
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
250251
meth = getattr(self, name, None)
251252
if meth:
252253
return meth(skipna=skipna, **kwargs)

pandas/core/arrays/base.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class ExtensionArray:
173173
# ------------------------------------------------------------------------
174174

175175
@classmethod
176-
def _from_sequence(cls, scalars, dtype=None, copy=False):
176+
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
177177
"""
178178
Construct a new ExtensionArray from a sequence of scalars.
179179
@@ -195,7 +195,7 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
195195
raise AbstractMethodError(cls)
196196

197197
@classmethod
198-
def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):
198+
def _from_sequence_of_strings(cls, strings, *, dtype=None, copy=False):
199199
"""
200200
Construct a new ExtensionArray from a sequence of strings.
201201
@@ -922,7 +922,11 @@ def repeat(self, repeats, axis=None):
922922
# ------------------------------------------------------------------------
923923

924924
def take(
925-
self, indices: Sequence[int], allow_fill: bool = False, fill_value: Any = None
925+
self,
926+
indices: Sequence[int],
927+
*,
928+
allow_fill: bool = False,
929+
fill_value: Any = None,
926930
) -> "ExtensionArray":
927931
"""
928932
Take elements from an array.
@@ -1153,7 +1157,7 @@ def _concat_same_type(
11531157
# of objects
11541158
_can_hold_na = True
11551159

1156-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
1160+
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
11571161
"""
11581162
Return a scalar result of performing the reduction operation.
11591163

pandas/core/arrays/boolean.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,17 @@ def dtype(self) -> BooleanDtype:
273273
return self._dtype
274274

275275
@classmethod
276-
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "BooleanArray":
276+
def _from_sequence(
277+
cls, scalars, *, dtype=None, copy: bool = False
278+
) -> "BooleanArray":
277279
if dtype:
278280
assert dtype == "boolean"
279281
values, mask = coerce_to_array(scalars, copy=copy)
280282
return BooleanArray(values, mask)
281283

282284
@classmethod
283285
def _from_sequence_of_strings(
284-
cls, strings: List[str], dtype=None, copy: bool = False
286+
cls, strings: List[str], *, dtype=None, copy: bool = False
285287
) -> "BooleanArray":
286288
def map_string(s):
287289
if isna(s):
@@ -294,7 +296,7 @@ def map_string(s):
294296
raise ValueError(f"{s} cannot be cast to bool")
295297

296298
scalars = [map_string(x) for x in strings]
297-
return cls._from_sequence(scalars, dtype, copy)
299+
return cls._from_sequence(scalars, dtype=dtype, copy=copy)
298300

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

@@ -682,12 +684,12 @@ def _arith_method(self, other, op):
682684

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

685-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
687+
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
686688

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

690-
return super()._reduce(name, skipna, **kwargs)
692+
return super()._reduce(name, skipna=skipna, **kwargs)
691693

692694
def _maybe_mask_result(self, result, mask, other, op_name: str):
693695
"""

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def _constructor(self) -> Type["Categorical"]:
385385
return Categorical
386386

387387
@classmethod
388-
def _from_sequence(cls, scalars, dtype=None, copy=False):
388+
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
389389
return Categorical(scalars, dtype=dtype)
390390

391391
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def _simple_new(
301301
return result
302302

303303
@classmethod
304-
def _from_sequence(cls, scalars, dtype=None, copy: bool = False):
304+
def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False):
305305
return cls._from_sequence_not_strict(scalars, dtype=dtype, copy=copy)
306306

307307
@classmethod

pandas/core/arrays/floating.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,18 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
275275
super().__init__(values, mask, copy=copy)
276276

277277
@classmethod
278-
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "FloatingArray":
278+
def _from_sequence(
279+
cls, scalars, *, dtype=None, copy: bool = False
280+
) -> "FloatingArray":
279281
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
280282
return FloatingArray(values, mask)
281283

282284
@classmethod
283285
def _from_sequence_of_strings(
284-
cls, strings, dtype=None, copy: bool = False
286+
cls, strings, *, dtype=None, copy: bool = False
285287
) -> "FloatingArray":
286288
scalars = to_numeric(strings, errors="raise")
287-
return cls._from_sequence(scalars, dtype, copy)
289+
return cls._from_sequence(scalars, dtype=dtype, copy=copy)
288290

289291
_HANDLED_TYPES = (np.ndarray, numbers.Number)
290292

pandas/core/arrays/integer.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,17 @@ def __abs__(self):
358358
return type(self)(np.abs(self._data), self._mask)
359359

360360
@classmethod
361-
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "IntegerArray":
361+
def _from_sequence(
362+
cls, scalars, *, dtype=None, copy: bool = False
363+
) -> "IntegerArray":
362364
return integer_array(scalars, dtype=dtype, copy=copy)
363365

364366
@classmethod
365367
def _from_sequence_of_strings(
366-
cls, strings, dtype=None, copy: bool = False
368+
cls, strings, *, dtype=None, copy: bool = False
367369
) -> "IntegerArray":
368370
scalars = to_numeric(strings, errors="raise")
369-
return cls._from_sequence(scalars, dtype, copy)
371+
return cls._from_sequence(scalars, dtype=dtype, copy=copy)
370372

371373
_HANDLED_TYPES = (np.ndarray, numbers.Number)
372374

pandas/core/arrays/interval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def _simple_new(cls, data, closed="right"):
227227
return result
228228

229229
@classmethod
230-
def _from_sequence(cls, scalars, dtype=None, copy=False):
230+
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
231231
return cls(scalars, dtype=dtype, copy=copy)
232232

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

791-
def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):
791+
def take(self, indices, *, allow_fill=False, fill_value=None, axis=None, **kwargs):
792792
"""
793793
Take elements from the IntervalArray.
794794

pandas/core/arrays/masked.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ def _concat_same_type(cls: Type[BaseMaskedArrayT], to_concat) -> BaseMaskedArray
269269
def take(
270270
self: BaseMaskedArrayT,
271271
indexer,
272+
*,
272273
allow_fill: bool = False,
273274
fill_value: Optional[Scalar] = None,
274275
) -> BaseMaskedArrayT:
@@ -357,7 +358,7 @@ def value_counts(self, dropna: bool = True) -> "Series":
357358

358359
return Series(counts, index=index)
359360

360-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
361+
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
361362
data = self._data
362363
mask = self._mask
363364

pandas/core/arrays/numpy_.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ def __init__(self, values: Union[np.ndarray, "PandasArray"], copy: bool = False)
172172
self._dtype = PandasDtype(values.dtype)
173173

174174
@classmethod
175-
def _from_sequence(cls, scalars, dtype=None, copy: bool = False) -> "PandasArray":
175+
def _from_sequence(
176+
cls, scalars, *, dtype=None, copy: bool = False
177+
) -> "PandasArray":
176178
if isinstance(dtype, PandasDtype):
177179
dtype = dtype._dtype
178180

pandas/core/arrays/period.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def _simple_new(
192192
def _from_sequence(
193193
cls: Type["PeriodArray"],
194194
scalars: Union[Sequence[Optional[Period]], AnyArrayLike],
195+
*,
195196
dtype: Optional[PeriodDtype] = None,
196197
copy: bool = False,
197198
) -> "PeriodArray":
@@ -214,9 +215,9 @@ def _from_sequence(
214215

215216
@classmethod
216217
def _from_sequence_of_strings(
217-
cls, strings, dtype=None, copy=False
218+
cls, strings, *, dtype=None, copy=False
218219
) -> "PeriodArray":
219-
return cls._from_sequence(strings, dtype, copy)
220+
return cls._from_sequence(strings, dtype=dtype, copy=copy)
220221

221222
@classmethod
222223
def _from_datetime64(cls, data, freq, tz=None) -> "PeriodArray":

pandas/core/arrays/sparse/array.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def __setitem__(self, key, value):
484484
raise TypeError(msg)
485485

486486
@classmethod
487-
def _from_sequence(cls, scalars, dtype=None, copy=False):
487+
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
488488
return cls(scalars, dtype=dtype)
489489

490490
@classmethod
@@ -809,7 +809,7 @@ def _get_val_at(self, loc):
809809
val = maybe_box_datetimelike(val, self.sp_values.dtype)
810810
return val
811811

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

1159-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
1159+
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
11601160
method = getattr(self, name, None)
11611161

11621162
if method is None:

pandas/core/arrays/string_.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def _validate(self):
198198
)
199199

200200
@classmethod
201-
def _from_sequence(cls, scalars, dtype=None, copy=False):
201+
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
202202
if dtype:
203203
assert dtype == "string"
204204

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

228228
@classmethod
229-
def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):
229+
def _from_sequence_of_strings(cls, strings, *, dtype=None, copy=False):
230230
return cls._from_sequence(strings, dtype=dtype, copy=copy)
231231

232232
def __arrow_array__(self, type=None):
@@ -295,7 +295,7 @@ def astype(self, dtype, copy=True):
295295

296296
return super().astype(dtype, copy)
297297

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

pandas/core/arrays/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _simple_new(
219219

220220
@classmethod
221221
def _from_sequence(
222-
cls, data, dtype=TD64NS_DTYPE, copy: bool = False
222+
cls, data, *, dtype=TD64NS_DTYPE, copy: bool = False
223223
) -> "TimedeltaArray":
224224
if dtype:
225225
_validate_td64_dtype(dtype)

pandas/core/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ def _reduce(
785785
self,
786786
op,
787787
name: str,
788+
*,
788789
axis=0,
789790
skipna=True,
790791
numeric_only=None,

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -8719,6 +8719,7 @@ def _reduce(
87198719
self,
87208720
op,
87218721
name: str,
8722+
*,
87228723
axis=0,
87238724
skipna=True,
87248725
numeric_only=None,

pandas/core/series.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -4190,7 +4190,15 @@ def f(x):
41904190
)
41914191

41924192
def _reduce(
4193-
self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds
4193+
self,
4194+
op,
4195+
name: str,
4196+
*,
4197+
axis=0,
4198+
skipna=True,
4199+
numeric_only=None,
4200+
filter_type=None,
4201+
**kwds,
41944202
):
41954203
"""
41964204
Perform a reduction operation.

pandas/tests/extension/arrow/arrays.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def _concat_same_type(cls, to_concat):
159159
def __invert__(self):
160160
return type(self).from_scalars(~self._data.to_pandas())
161161

162-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
162+
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
163163
if skipna:
164164
arr = self[~self.isna()]
165165
else:

pandas/tests/extension/decimal/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def _formatter(self, boxed=False):
178178
def _concat_same_type(cls, to_concat):
179179
return cls(np.concatenate([x._data for x in to_concat]))
180180

181-
def _reduce(self, name: str, skipna: bool = True, **kwargs):
181+
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
182182

183183
if skipna:
184184
# If we don't have any NAs, we can ignore skipna

0 commit comments

Comments
 (0)