Skip to content

Commit 28710b3

Browse files
jbrockmendelproost
authored andcommitted
CLN: type annotations (pandas-dev#29333)
1 parent 76df310 commit 28710b3

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ def nsmallest(self):
10891089
return self.compute("nsmallest")
10901090

10911091
@staticmethod
1092-
def is_valid_dtype_n_method(dtype):
1092+
def is_valid_dtype_n_method(dtype) -> bool:
10931093
"""
10941094
Helper function to determine if dtype is valid for
10951095
nsmallest/nlargest methods

pandas/core/dtypes/cast.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def _ensure_dtype_type(value, dtype):
495495
return dtype.type(value)
496496

497497

498-
def infer_dtype_from(val, pandas_dtype=False):
498+
def infer_dtype_from(val, pandas_dtype: bool = False):
499499
"""
500500
interpret the dtype from a scalar or array. This is a convenience
501501
routines to infer dtype from a scalar or an array
@@ -512,7 +512,7 @@ def infer_dtype_from(val, pandas_dtype=False):
512512
return infer_dtype_from_array(val, pandas_dtype=pandas_dtype)
513513

514514

515-
def infer_dtype_from_scalar(val, pandas_dtype=False):
515+
def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
516516
"""
517517
interpret the dtype from a scalar
518518
@@ -587,7 +587,7 @@ def infer_dtype_from_scalar(val, pandas_dtype=False):
587587
return dtype, val
588588

589589

590-
def infer_dtype_from_array(arr, pandas_dtype=False):
590+
def infer_dtype_from_array(arr, pandas_dtype: bool = False):
591591
"""
592592
infer the dtype from a scalar or array
593593

pandas/core/reshape/tile.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""
22
Quantilization functions and related stuff
33
"""
4-
from functools import partial
5-
64
import numpy as np
75

86
from pandas._libs import Timedelta, Timestamp
@@ -38,12 +36,12 @@
3836
def cut(
3937
x,
4038
bins,
41-
right=True,
39+
right: bool = True,
4240
labels=None,
43-
retbins=False,
44-
precision=3,
45-
include_lowest=False,
46-
duplicates="raise",
41+
retbins: bool = False,
42+
precision: int = 3,
43+
include_lowest: bool = False,
44+
duplicates: str = "raise",
4745
):
4846
"""
4947
Bin values into discrete intervals.
@@ -275,7 +273,14 @@ def cut(
275273
)
276274

277275

278-
def qcut(x, q, labels=None, retbins=False, precision=3, duplicates="raise"):
276+
def qcut(
277+
x,
278+
q,
279+
labels=None,
280+
retbins: bool = False,
281+
precision: int = 3,
282+
duplicates: str = "raise",
283+
):
279284
"""
280285
Quantile-based discretization function. Discretize variable into
281286
equal-sized buckets based on rank or based on sample quantiles. For example
@@ -355,12 +360,12 @@ def qcut(x, q, labels=None, retbins=False, precision=3, duplicates="raise"):
355360
def _bins_to_cuts(
356361
x,
357362
bins,
358-
right=True,
363+
right: bool = True,
359364
labels=None,
360-
precision=3,
361-
include_lowest=False,
365+
precision: int = 3,
366+
include_lowest: bool = False,
362367
dtype=None,
363-
duplicates="raise",
368+
duplicates: str = "raise",
364369
):
365370

366371
if duplicates not in ["raise", "drop"]:
@@ -498,13 +503,15 @@ def _convert_bin_to_datelike_type(bins, dtype):
498503
return bins
499504

500505

501-
def _format_labels(bins, precision, right=True, include_lowest=False, dtype=None):
506+
def _format_labels(
507+
bins, precision, right: bool = True, include_lowest: bool = False, dtype=None
508+
):
502509
""" based on the dtype, return our labels """
503510

504511
closed = "right" if right else "left"
505512

506513
if is_datetime64tz_dtype(dtype):
507-
formatter = partial(Timestamp, tz=dtype.tz)
514+
formatter = lambda x: Timestamp(x, tz=dtype.tz)
508515
adjust = lambda x: x - Timedelta("1ns")
509516
elif is_datetime64_dtype(dtype):
510517
formatter = Timestamp
@@ -556,7 +563,9 @@ def _preprocess_for_cut(x):
556563
return x_is_series, series_index, name, x
557564

558565

559-
def _postprocess_for_cut(fac, bins, retbins, x_is_series, series_index, name, dtype):
566+
def _postprocess_for_cut(
567+
fac, bins, retbins: bool, x_is_series, series_index, name, dtype
568+
):
560569
"""
561570
handles post processing for the cut method where
562571
we combine the index information if the originally passed

pandas/core/sorting.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
_INT64_MAX = np.iinfo(np.int64).max
2121

2222

23-
def get_group_index(labels, shape, sort, xnull):
23+
def get_group_index(labels, shape, sort: bool, xnull: bool):
2424
"""
2525
For the particular label_list, gets the offsets into the hypothetical list
2626
representing the totally ordered cartesian product of all possible label
@@ -48,7 +48,7 @@ def get_group_index(labels, shape, sort, xnull):
4848
labels are equal at all location.
4949
"""
5050

51-
def _int64_cut_off(shape):
51+
def _int64_cut_off(shape) -> int:
5252
acc = 1
5353
for i, mul in enumerate(shape):
5454
acc *= int(mul)
@@ -125,7 +125,7 @@ def get_compressed_ids(labels, sizes):
125125
return compress_group_index(ids, sort=True)
126126

127127

128-
def is_int64_overflow_possible(shape):
128+
def is_int64_overflow_possible(shape) -> bool:
129129
the_prod = 1
130130
for x in shape:
131131
the_prod *= int(x)
@@ -153,7 +153,7 @@ def decons_group_index(comp_labels, shape):
153153
return label_list[::-1]
154154

155155

156-
def decons_obs_group_ids(comp_ids, obs_ids, shape, labels, xnull):
156+
def decons_obs_group_ids(comp_ids, obs_ids, shape, labels, xnull: bool):
157157
"""
158158
reconstruct labels from observed group ids
159159
@@ -177,7 +177,7 @@ def decons_obs_group_ids(comp_ids, obs_ids, shape, labels, xnull):
177177
return [i8copy(lab[i]) for lab in labels]
178178

179179

180-
def indexer_from_factorized(labels, shape, compress=True):
180+
def indexer_from_factorized(labels, shape, compress: bool = True):
181181
ids = get_group_index(labels, shape, sort=True, xnull=False)
182182

183183
if not compress:
@@ -235,7 +235,7 @@ def lexsort_indexer(keys, orders=None, na_position="last"):
235235
return indexer_from_factorized(labels, shape)
236236

237237

238-
def nargsort(items, kind="quicksort", ascending=True, na_position="last"):
238+
def nargsort(items, kind="quicksort", ascending: bool = True, na_position="last"):
239239
"""
240240
This is intended to be a drop-in replacement for np.argsort which
241241
handles NaNs. It adds ascending and na_position parameters.
@@ -325,7 +325,7 @@ def get_indexer_dict(label_list, keys):
325325
# sorting levels...cleverly?
326326

327327

328-
def get_group_index_sorter(group_index, ngroups):
328+
def get_group_index_sorter(group_index, ngroups: int):
329329
"""
330330
algos.groupsort_indexer implements `counting sort` and it is at least
331331
O(ngroups), where
@@ -350,7 +350,7 @@ def get_group_index_sorter(group_index, ngroups):
350350
return group_index.argsort(kind="mergesort")
351351

352352

353-
def compress_group_index(group_index, sort=True):
353+
def compress_group_index(group_index, sort: bool = True):
354354
"""
355355
Group_index is offsets into cartesian product of all possible labels. This
356356
space can be huge, so this function compresses it, by computing offsets
@@ -391,7 +391,13 @@ def _reorder_by_uniques(uniques, labels):
391391
return uniques, labels
392392

393393

394-
def safe_sort(values, labels=None, na_sentinel=-1, assume_unique=False, verify=True):
394+
def safe_sort(
395+
values,
396+
labels=None,
397+
na_sentinel: int = -1,
398+
assume_unique: bool = False,
399+
verify: bool = True,
400+
):
395401
"""
396402
Sort ``values`` and reorder corresponding ``labels``.
397403
``values`` should be unique if ``labels`` is not None.

0 commit comments

Comments
 (0)