Skip to content

Commit

Permalink
CLN: Assorted typings (pandas-dev#28604)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent c8ce3ca commit e2794ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
24 changes: 12 additions & 12 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,14 @@ def _reconstruct_data(values, dtype, original):
-------
Index for extension types, otherwise ndarray casted to dtype
"""
from pandas import Index

if is_extension_array_dtype(dtype):
values = dtype.construct_array_type()._from_sequence(values)
elif is_bool_dtype(dtype):
values = values.astype(dtype)

# we only support object dtypes bool Index
if isinstance(original, Index):
if isinstance(original, ABCIndexClass):
values = values.astype(object)
elif dtype is not None:
values = values.astype(dtype)
Expand Down Expand Up @@ -833,7 +832,7 @@ def duplicated(values, keep="first"):
return f(values, keep=keep)


def mode(values, dropna=True):
def mode(values, dropna: bool = True):
"""
Returns the mode(s) of an array.
Expand Down Expand Up @@ -1888,7 +1887,7 @@ def searchsorted(arr, value, side="left", sorter=None):
}


def diff(arr, n, axis=0):
def diff(arr, n: int, axis: int = 0):
"""
difference of n between self,
analogous to s-s.shift(n)
Expand All @@ -1904,7 +1903,6 @@ def diff(arr, n, axis=0):
Returns
-------
shifted
"""

n = int(n)
Expand Down Expand Up @@ -1935,13 +1933,15 @@ def diff(arr, n, axis=0):
f = _diff_special[arr.dtype.name]
f(arr, out_arr, n, axis)
else:
res_indexer = [slice(None)] * arr.ndim
res_indexer[axis] = slice(n, None) if n >= 0 else slice(None, n)
res_indexer = tuple(res_indexer)

lag_indexer = [slice(None)] * arr.ndim
lag_indexer[axis] = slice(None, -n) if n > 0 else slice(-n, None)
lag_indexer = tuple(lag_indexer)
# To keep mypy happy, _res_indexer is a list while res_indexer is
# a tuple, ditto for lag_indexer.
_res_indexer = [slice(None)] * arr.ndim
_res_indexer[axis] = slice(n, None) if n >= 0 else slice(None, n)
res_indexer = tuple(_res_indexer)

_lag_indexer = [slice(None)] * arr.ndim
_lag_indexer[axis] = slice(None, -n) if n > 0 else slice(-n, None)
lag_indexer = tuple(_lag_indexer)

# need to make sure that we account for na for datelike/timedelta
# we don't actually want to subtract these i8 numbers
Expand Down
21 changes: 14 additions & 7 deletions pandas/core/util/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
_default_hash_key = "0123456789123456"


def _combine_hash_arrays(arrays, num_items):
def _combine_hash_arrays(arrays, num_items: int):
"""
Parameters
----------
Expand Down Expand Up @@ -55,7 +55,11 @@ def _combine_hash_arrays(arrays, num_items):


def hash_pandas_object(
obj, index=True, encoding="utf8", hash_key=None, categorize=True
obj,
index: bool = True,
encoding: str = "utf8",
hash_key=None,
categorize: bool = True,
):
"""
Return a data hash of the Index/Series/DataFrame.
Expand Down Expand Up @@ -125,7 +129,10 @@ def hash_pandas_object(
for _ in [None]
)
num_items += 1
hashes = itertools.chain(hashes, index_hash_generator)

# keep `hashes` specifically a generator to keep mypy happy
_hashes = itertools.chain(hashes, index_hash_generator)
hashes = (x for x in _hashes)
h = _combine_hash_arrays(hashes, num_items)

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


def hash_tuple(val, encoding="utf8", hash_key=None):
def hash_tuple(val, encoding: str = "utf8", hash_key=None):
"""
Hash a single tuple efficiently
Expand All @@ -201,7 +208,7 @@ def hash_tuple(val, encoding="utf8", hash_key=None):
return h


def _hash_categorical(c, encoding, hash_key):
def _hash_categorical(c, encoding: str, hash_key: str):
"""
Hash a Categorical by hashing its categories, and then mapping the codes
to the hashes
Expand Down Expand Up @@ -239,7 +246,7 @@ def _hash_categorical(c, encoding, hash_key):
return result


def hash_array(vals, encoding="utf8", hash_key=None, categorize=True):
def hash_array(vals, encoding: str = "utf8", hash_key=None, categorize: bool = True):
"""
Given a 1d array, return an array of deterministic integers.
Expand Down Expand Up @@ -317,7 +324,7 @@ def hash_array(vals, encoding="utf8", hash_key=None, categorize=True):
return vals


def _hash_scalar(val, encoding="utf8", hash_key=None):
def _hash_scalar(val, encoding: str = "utf8", hash_key=None):
"""
Hash scalar value
Expand Down

0 comments on commit e2794ac

Please sign in to comment.