Skip to content

Commit 152c987

Browse files
jbrockmendelNico Cernek
authored and
Nico Cernek
committed
CLN: Assorted typings (pandas-dev#28604)
1 parent e6cacf5 commit 152c987

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

pandas/core/algorithms.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,14 @@ def _reconstruct_data(values, dtype, original):
176176
-------
177177
Index for extension types, otherwise ndarray casted to dtype
178178
"""
179-
from pandas import Index
180179

181180
if is_extension_array_dtype(dtype):
182181
values = dtype.construct_array_type()._from_sequence(values)
183182
elif is_bool_dtype(dtype):
184183
values = values.astype(dtype)
185184

186185
# we only support object dtypes bool Index
187-
if isinstance(original, Index):
186+
if isinstance(original, ABCIndexClass):
188187
values = values.astype(object)
189188
elif dtype is not None:
190189
values = values.astype(dtype)
@@ -833,7 +832,7 @@ def duplicated(values, keep="first"):
833832
return f(values, keep=keep)
834833

835834

836-
def mode(values, dropna=True):
835+
def mode(values, dropna: bool = True):
837836
"""
838837
Returns the mode(s) of an array.
839838
@@ -1888,7 +1887,7 @@ def searchsorted(arr, value, side="left", sorter=None):
18881887
}
18891888

18901889

1891-
def diff(arr, n, axis=0):
1890+
def diff(arr, n: int, axis: int = 0):
18921891
"""
18931892
difference of n between self,
18941893
analogous to s-s.shift(n)
@@ -1904,7 +1903,6 @@ def diff(arr, n, axis=0):
19041903
Returns
19051904
-------
19061905
shifted
1907-
19081906
"""
19091907

19101908
n = int(n)
@@ -1935,13 +1933,15 @@ def diff(arr, n, axis=0):
19351933
f = _diff_special[arr.dtype.name]
19361934
f(arr, out_arr, n, axis)
19371935
else:
1938-
res_indexer = [slice(None)] * arr.ndim
1939-
res_indexer[axis] = slice(n, None) if n >= 0 else slice(None, n)
1940-
res_indexer = tuple(res_indexer)
1941-
1942-
lag_indexer = [slice(None)] * arr.ndim
1943-
lag_indexer[axis] = slice(None, -n) if n > 0 else slice(-n, None)
1944-
lag_indexer = tuple(lag_indexer)
1936+
# To keep mypy happy, _res_indexer is a list while res_indexer is
1937+
# a tuple, ditto for lag_indexer.
1938+
_res_indexer = [slice(None)] * arr.ndim
1939+
_res_indexer[axis] = slice(n, None) if n >= 0 else slice(None, n)
1940+
res_indexer = tuple(_res_indexer)
1941+
1942+
_lag_indexer = [slice(None)] * arr.ndim
1943+
_lag_indexer[axis] = slice(None, -n) if n > 0 else slice(-n, None)
1944+
lag_indexer = tuple(_lag_indexer)
19451945

19461946
# need to make sure that we account for na for datelike/timedelta
19471947
# we don't actually want to subtract these i8 numbers

pandas/core/util/hashing.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
_default_hash_key = "0123456789123456"
2727

2828

29-
def _combine_hash_arrays(arrays, num_items):
29+
def _combine_hash_arrays(arrays, num_items: int):
3030
"""
3131
Parameters
3232
----------
@@ -55,7 +55,11 @@ def _combine_hash_arrays(arrays, num_items):
5555

5656

5757
def hash_pandas_object(
58-
obj, index=True, encoding="utf8", hash_key=None, categorize=True
58+
obj,
59+
index: bool = True,
60+
encoding: str = "utf8",
61+
hash_key=None,
62+
categorize: bool = True,
5963
):
6064
"""
6165
Return a data hash of the Index/Series/DataFrame.
@@ -125,7 +129,10 @@ def hash_pandas_object(
125129
for _ in [None]
126130
)
127131
num_items += 1
128-
hashes = itertools.chain(hashes, index_hash_generator)
132+
133+
# keep `hashes` specifically a generator to keep mypy happy
134+
_hashes = itertools.chain(hashes, index_hash_generator)
135+
hashes = (x for x in _hashes)
129136
h = _combine_hash_arrays(hashes, num_items)
130137

131138
h = Series(h, index=obj.index, dtype="uint64", copy=False)
@@ -179,7 +186,7 @@ def hash_tuples(vals, encoding="utf8", hash_key=None):
179186
return h
180187

181188

182-
def hash_tuple(val, encoding="utf8", hash_key=None):
189+
def hash_tuple(val, encoding: str = "utf8", hash_key=None):
183190
"""
184191
Hash a single tuple efficiently
185192
@@ -201,7 +208,7 @@ def hash_tuple(val, encoding="utf8", hash_key=None):
201208
return h
202209

203210

204-
def _hash_categorical(c, encoding, hash_key):
211+
def _hash_categorical(c, encoding: str, hash_key: str):
205212
"""
206213
Hash a Categorical by hashing its categories, and then mapping the codes
207214
to the hashes
@@ -239,7 +246,7 @@ def _hash_categorical(c, encoding, hash_key):
239246
return result
240247

241248

242-
def hash_array(vals, encoding="utf8", hash_key=None, categorize=True):
249+
def hash_array(vals, encoding: str = "utf8", hash_key=None, categorize: bool = True):
243250
"""
244251
Given a 1d array, return an array of deterministic integers.
245252
@@ -317,7 +324,7 @@ def hash_array(vals, encoding="utf8", hash_key=None, categorize=True):
317324
return vals
318325

319326

320-
def _hash_scalar(val, encoding="utf8", hash_key=None):
327+
def _hash_scalar(val, encoding: str = "utf8", hash_key=None):
321328
"""
322329
Hash scalar value
323330

0 commit comments

Comments
 (0)