Skip to content

Commit 12594f3

Browse files
jbrockmendelJulianWgs
authored andcommitted
TYP: mostly core.arrays, some core.indexes (pandas-dev#40545)
1 parent 7cb8449 commit 12594f3

15 files changed

+185
-111
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

+6-6
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,15 @@ cdef class {{name}}HashTable(HashTable):
411411
k = kh_get_{{dtype}}(self.table, ckey)
412412
return k != self.table.n_buckets
413413

414-
def sizeof(self, deep=False):
414+
def sizeof(self, deep: bool = False) -> int:
415415
""" return the size of my table in bytes """
416416
overhead = 4 * sizeof(uint32_t) + 3 * sizeof(uint32_t*)
417417
for_flags = max(1, self.table.n_buckets >> 5) * sizeof(uint32_t)
418418
for_pairs = self.table.n_buckets * (sizeof({{dtype}}_t) + # keys
419419
sizeof(Py_ssize_t)) # vals
420420
return overhead + for_flags + for_pairs
421421

422-
def get_state(self):
422+
def get_state(self) -> dict[str, int]:
423423
""" returns infos about the state of the hashtable"""
424424
return {
425425
'n_buckets' : self.table.n_buckets,
@@ -747,14 +747,14 @@ cdef class StringHashTable(HashTable):
747747
kh_destroy_str(self.table)
748748
self.table = NULL
749749

750-
def sizeof(self, deep=False):
750+
def sizeof(self, deep: bool = False) -> int:
751751
overhead = 4 * sizeof(uint32_t) + 3 * sizeof(uint32_t*)
752752
for_flags = max(1, self.table.n_buckets >> 5) * sizeof(uint32_t)
753753
for_pairs = self.table.n_buckets * (sizeof(char *) + # keys
754754
sizeof(Py_ssize_t)) # vals
755755
return overhead + for_flags + for_pairs
756756

757-
def get_state(self):
757+
def get_state(self) -> dict[str, int]:
758758
""" returns infos about the state of the hashtable"""
759759
return {
760760
'n_buckets' : self.table.n_buckets,
@@ -1079,15 +1079,15 @@ cdef class PyObjectHashTable(HashTable):
10791079
k = kh_get_pymap(self.table, <PyObject*>key)
10801080
return k != self.table.n_buckets
10811081

1082-
def sizeof(self, deep=False):
1082+
def sizeof(self, deep: bool = False) -> int:
10831083
""" return the size of my table in bytes """
10841084
overhead = 4 * sizeof(uint32_t) + 3 * sizeof(uint32_t*)
10851085
for_flags = max(1, self.table.n_buckets >> 5) * sizeof(uint32_t)
10861086
for_pairs = self.table.n_buckets * (sizeof(PyObject *) + # keys
10871087
sizeof(Py_ssize_t)) # vals
10881088
return overhead + for_flags + for_pairs
10891089

1090-
def get_state(self):
1090+
def get_state(self) -> dict[str, int]:
10911091
"""
10921092
returns infos about the current state of the hashtable like size,
10931093
number of buckets and so on.

pandas/_libs/ops.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def vec_binop(object[:] left, object[:] right, object op) -> ndarray:
258258

259259

260260
def maybe_convert_bool(ndarray[object] arr,
261-
true_values=None, false_values=None):
261+
true_values=None, false_values=None) -> ndarray:
262262
cdef:
263263
Py_ssize_t i, n
264264
ndarray[uint8_t] result

pandas/_libs/tslibs/timedeltas.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ cdef convert_to_timedelta64(object ts, str unit):
311311

312312
@cython.boundscheck(False)
313313
@cython.wraparound(False)
314-
def array_to_timedelta64(ndarray[object] values, str unit=None, str errors="raise"):
314+
def array_to_timedelta64(
315+
ndarray[object] values, str unit=None, str errors="raise"
316+
) -> ndarray:
315317
"""
316318
Convert an ndarray to an array of timedeltas. If errors == 'coerce',
317319
coerce non-convertible objects to NaT. Otherwise, raise.

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ def searchsorted(arr, value, side="left", sorter=None) -> np.ndarray:
16191619
_diff_special = {"float64", "float32", "int64", "int32", "int16", "int8"}
16201620

16211621

1622-
def diff(arr, n: int, axis: int = 0, stacklevel=3):
1622+
def diff(arr, n: int, axis: int = 0, stacklevel: int = 3):
16231623
"""
16241624
difference of n between self,
16251625
analogous to s-s.shift(n)
@@ -1865,7 +1865,7 @@ def safe_sort(
18651865
return ordered, ensure_platform_int(new_codes)
18661866

18671867

1868-
def _sort_mixed(values):
1868+
def _sort_mixed(values) -> np.ndarray:
18691869
""" order ints before strings in 1d arrays, safe in py3 """
18701870
str_pos = np.array([isinstance(x, str) for x in values], dtype=bool)
18711871
nums = np.sort(values[~str_pos])

pandas/core/arrays/_mixins.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ def ndim(self) -> int:
157157

158158
@cache_readonly
159159
def size(self) -> int:
160-
# error: Incompatible return value type (got "number", expected "int")
161-
return np.prod(self.shape) # type: ignore[return-value]
160+
return self._ndarray.size
162161

163162
@cache_readonly
164163
def nbytes(self) -> int:
@@ -190,7 +189,7 @@ def equals(self, other) -> bool:
190189
return False
191190
return bool(array_equivalent(self._ndarray, other._ndarray))
192191

193-
def _values_for_argsort(self):
192+
def _values_for_argsort(self) -> np.ndarray:
194193
return self._ndarray
195194

196195
# Signature of "argmin" incompatible with supertype "ExtensionArray"

pandas/core/arrays/categorical.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ def _from_inferred_categories(
606606
if true_values is None:
607607
true_values = ["True", "TRUE", "true"]
608608

609-
cats = cats.isin(true_values)
609+
# error: Incompatible types in assignment (expression has type
610+
# "ndarray", variable has type "Index")
611+
cats = cats.isin(true_values) # type: ignore[assignment]
610612

611613
if known_categories:
612614
# Recode from observation order to dtype.categories order.
@@ -1444,7 +1446,7 @@ def memory_usage(self, deep: bool = False) -> int:
14441446
"""
14451447
return self._codes.nbytes + self.dtype.categories.memory_usage(deep=deep)
14461448

1447-
def isna(self):
1449+
def isna(self) -> np.ndarray:
14481450
"""
14491451
Detect missing values
14501452
@@ -1465,7 +1467,7 @@ def isna(self):
14651467

14661468
isnull = isna
14671469

1468-
def notna(self):
1470+
def notna(self) -> np.ndarray:
14691471
"""
14701472
Inverse of isna
14711473
@@ -1731,7 +1733,7 @@ def view(self, dtype=None):
17311733
raise NotImplementedError(dtype)
17321734
return self._from_backing_data(self._ndarray)
17331735

1734-
def to_dense(self):
1736+
def to_dense(self) -> np.ndarray:
17351737
"""
17361738
Return my 'dense' representation
17371739
@@ -1804,14 +1806,14 @@ def __contains__(self, key) -> bool:
18041806
"""
18051807
# if key is a NaN, check if any NaN is in self.
18061808
if is_valid_na_for_dtype(key, self.categories.dtype):
1807-
return self.isna().any()
1809+
return bool(self.isna().any())
18081810

18091811
return contains(self, key, container=self._codes)
18101812

18111813
# ------------------------------------------------------------------
18121814
# Rendering Methods
18131815

1814-
def _formatter(self, boxed=False):
1816+
def _formatter(self, boxed: bool = False):
18151817
# Defer to CategoricalFormatter's formatter.
18161818
return None
18171819

@@ -1889,7 +1891,7 @@ def _repr_footer(self) -> str:
18891891
info = self._repr_categories_info()
18901892
return f"Length: {len(self)}\n{info}"
18911893

1892-
def _get_repr(self, length=True, na_rep="NaN", footer=True) -> str:
1894+
def _get_repr(self, length: bool = True, na_rep="NaN", footer: bool = True) -> str:
18931895
from pandas.io.formats import format as fmt
18941896

18951897
formatter = fmt.CategoricalFormatter(

pandas/core/arrays/datetimelike.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def _format_native_types(self, na_rep="NaT", date_format=None):
327327
"""
328328
raise AbstractMethodError(self)
329329

330-
def _formatter(self, boxed=False):
330+
def _formatter(self, boxed: bool = False):
331331
# TODO: Remove Datetime & DatetimeTZ formatters.
332332
return "'{}'".format
333333

@@ -354,7 +354,7 @@ def __getitem__(
354354
result._freq = self._get_getitem_freq(key)
355355
return result
356356

357-
def _get_getitem_freq(self, key):
357+
def _get_getitem_freq(self, key) -> Optional[BaseOffset]:
358358
"""
359359
Find the `freq` attribute to assign to the result of a __getitem__ lookup.
360360
"""
@@ -406,7 +406,7 @@ def _maybe_clear_freq(self):
406406
# DatetimeArray and TimedeltaArray
407407
pass
408408

409-
def astype(self, dtype, copy=True):
409+
def astype(self, dtype, copy: bool = True):
410410
# Some notes on cases we don't have to handle here in the base class:
411411
# 1. PeriodArray.astype handles period -> period
412412
# 2. DatetimeArray.astype handles conversion between tz.
@@ -545,7 +545,7 @@ def _values_for_factorize(self):
545545

546546
@classmethod
547547
def _from_factorized(
548-
cls: Type[DatetimeLikeArrayT], values, original
548+
cls: Type[DatetimeLikeArrayT], values, original: DatetimeLikeArrayT
549549
) -> DatetimeLikeArrayT:
550550
return cls(values, dtype=original.dtype)
551551

@@ -939,7 +939,7 @@ def freq(self, value):
939939
self._freq = value
940940

941941
@property
942-
def freqstr(self):
942+
def freqstr(self) -> Optional[str]:
943943
"""
944944
Return the frequency object as a string if its set, otherwise None.
945945
"""
@@ -948,7 +948,7 @@ def freqstr(self):
948948
return self.freq.freqstr
949949

950950
@property # NB: override with cache_readonly in immutable subclasses
951-
def inferred_freq(self):
951+
def inferred_freq(self) -> Optional[str]:
952952
"""
953953
Tries to return a string representing a frequency guess,
954954
generated by infer_freq. Returns None if it can't autodetect the
@@ -963,8 +963,11 @@ def inferred_freq(self):
963963

964964
@property # NB: override with cache_readonly in immutable subclasses
965965
def _resolution_obj(self) -> Optional[Resolution]:
966+
freqstr = self.freqstr
967+
if freqstr is None:
968+
return None
966969
try:
967-
return Resolution.get_reso_from_freq(self.freqstr)
970+
return Resolution.get_reso_from_freq(freqstr)
968971
except KeyError:
969972
return None
970973

@@ -1241,7 +1244,7 @@ def _addsub_object_array(self, other: np.ndarray, op):
12411244
)
12421245
return result
12431246

1244-
def _time_shift(self, periods, freq=None):
1247+
def _time_shift(self, periods: int, freq=None):
12451248
"""
12461249
Shift each value by `periods`.
12471250
@@ -1440,7 +1443,7 @@ def __isub__(self, other):
14401443
# --------------------------------------------------------------
14411444
# Reductions
14421445

1443-
def min(self, *, axis=None, skipna=True, **kwargs):
1446+
def min(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
14441447
"""
14451448
Return the minimum value of the Array or minimum along
14461449
an axis.
@@ -1469,7 +1472,7 @@ def min(self, *, axis=None, skipna=True, **kwargs):
14691472
result = nanops.nanmin(self._ndarray, axis=axis, skipna=skipna)
14701473
return self._wrap_reduction_result(axis, result)
14711474

1472-
def max(self, *, axis=None, skipna=True, **kwargs):
1475+
def max(self, *, axis: Optional[int] = None, skipna: bool = True, **kwargs):
14731476
"""
14741477
Return the maximum value of the Array or maximum along
14751478
an axis.
@@ -1500,7 +1503,7 @@ def max(self, *, axis=None, skipna=True, **kwargs):
15001503
result = nanops.nanmax(self._ndarray, axis=axis, skipna=skipna)
15011504
return self._wrap_reduction_result(axis, result)
15021505

1503-
def mean(self, *, skipna=True, axis: Optional[int] = 0):
1506+
def mean(self, *, skipna: bool = True, axis: Optional[int] = 0):
15041507
"""
15051508
Return the mean value of the Array.
15061509
@@ -1568,7 +1571,7 @@ class DatelikeOps(DatetimeLikeArrayMixin):
15681571
URL="https://docs.python.org/3/library/datetime.html"
15691572
"#strftime-and-strptime-behavior"
15701573
)
1571-
def strftime(self, date_format):
1574+
def strftime(self, date_format: str) -> np.ndarray:
15721575
"""
15731576
Convert to Index using specified date_format.
15741577
@@ -1760,7 +1763,7 @@ def all(self, *, axis: Optional[int] = None, skipna: bool = True):
17601763
# --------------------------------------------------------------
17611764
# Frequency Methods
17621765

1763-
def _maybe_clear_freq(self):
1766+
def _maybe_clear_freq(self) -> None:
17641767
self._freq = None
17651768

17661769
def _with_freq(self, freq):

0 commit comments

Comments
 (0)