Skip to content

Commit ea9114f

Browse files
jbrockmendelproost
authored andcommitted
CLN: Add types in a handful of places (pandas-dev#29178)
1 parent 7189570 commit ea9114f

File tree

6 files changed

+42
-33
lines changed

6 files changed

+42
-33
lines changed

pandas/_libs/index.pyx

+6-6
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ cdef class IndexEngine:
171171

172172
raise KeyError(val)
173173

174-
def sizeof(self, deep=False):
174+
def sizeof(self, deep: bool = False) -> int:
175175
""" return the sizeof our mapping """
176176
if not self.is_mapping_populated:
177177
return 0
178178
return self.mapping.sizeof(deep=deep)
179179

180-
def __sizeof__(self):
180+
def __sizeof__(self) -> int:
181181
return self.sizeof()
182182

183183
@property
184-
def is_unique(self):
184+
def is_unique(self) -> bool:
185185
if self.need_unique_check:
186186
self._do_unique_check()
187187

@@ -193,14 +193,14 @@ cdef class IndexEngine:
193193
self._ensure_mapping_populated()
194194

195195
@property
196-
def is_monotonic_increasing(self):
196+
def is_monotonic_increasing(self) -> bool:
197197
if self.need_monotonic_check:
198198
self._do_monotonic_check()
199199

200200
return self.monotonic_inc == 1
201201

202202
@property
203-
def is_monotonic_decreasing(self):
203+
def is_monotonic_decreasing(self) -> bool:
204204
if self.need_monotonic_check:
205205
self._do_monotonic_check()
206206

@@ -243,7 +243,7 @@ cdef class IndexEngine:
243243
hash(val)
244244

245245
@property
246-
def is_mapping_populated(self):
246+
def is_mapping_populated(self) -> bool:
247247
return self.mapping is not None
248248

249249
cdef inline _ensure_mapping_populated(self):

pandas/core/algorithms.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -692,30 +692,34 @@ def factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None):
692692

693693

694694
def value_counts(
695-
values, sort=True, ascending=False, normalize=False, bins=None, dropna=True
695+
values,
696+
sort: bool = True,
697+
ascending: bool = False,
698+
normalize: bool = False,
699+
bins=None,
700+
dropna: bool = True,
696701
):
697702
"""
698703
Compute a histogram of the counts of non-null values.
699704
700705
Parameters
701706
----------
702707
values : ndarray (1-d)
703-
sort : boolean, default True
708+
sort : bool, default True
704709
Sort by values
705-
ascending : boolean, default False
710+
ascending : bool, default False
706711
Sort in ascending order
707-
normalize: boolean, default False
712+
normalize: bool, default False
708713
If True then compute a relative histogram
709714
bins : integer, optional
710715
Rather than count values, group them into half-open bins,
711716
convenience for pd.cut, only works with numeric data
712-
dropna : boolean, default True
717+
dropna : bool, default True
713718
Don't include counts of NaN
714719
715720
Returns
716721
-------
717-
value_counts : Series
718-
722+
Series
719723
"""
720724
from pandas.core.series import Series, Index
721725

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ def _intersection_non_unique(self, other: "IntervalIndex") -> "IntervalIndex":
13401340

13411341
return self[mask]
13421342

1343-
def _setop(op_name, sort=None):
1343+
def _setop(op_name: str, sort=None):
13441344
@SetopCheck(op_name=op_name)
13451345
def func(self, other, sort=sort):
13461346
result = getattr(self._multiindex, op_name)(other._multiindex, sort=sort)

pandas/core/nanops.py

+20-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pandas._config import get_option
99

10-
from pandas._libs import iNaT, lib, tslibs
10+
from pandas._libs import NaT, Timedelta, Timestamp, iNaT, lib
1111
from pandas.compat._optional import import_optional_dependency
1212

1313
from pandas.core.dtypes.cast import _int64_max, maybe_upcast_putmask
@@ -53,7 +53,7 @@ def __init__(self, *dtypes):
5353
super().__init__()
5454
self.dtypes = tuple(pandas_dtype(dtype).type for dtype in dtypes)
5555

56-
def check(self, obj):
56+
def check(self, obj) -> bool:
5757
return hasattr(obj, "dtype") and issubclass(obj.dtype.type, self.dtypes)
5858

5959
def __call__(self, f):
@@ -128,7 +128,7 @@ def f(values, axis=None, skipna=True, **kwds):
128128
return f
129129

130130

131-
def _bn_ok_dtype(dt, name):
131+
def _bn_ok_dtype(dt, name: str) -> bool:
132132
# Bottleneck chokes on datetime64
133133
if not is_object_dtype(dt) and not (
134134
is_datetime_or_timedelta_dtype(dt) or is_datetime64tz_dtype(dt)
@@ -149,7 +149,7 @@ def _bn_ok_dtype(dt, name):
149149
return False
150150

151151

152-
def _has_infs(result):
152+
def _has_infs(result) -> bool:
153153
if isinstance(result, np.ndarray):
154154
if result.dtype == "f8":
155155
return lib.has_infs_f8(result.ravel())
@@ -176,19 +176,22 @@ def _get_fill_value(dtype, fill_value=None, fill_value_typ=None):
176176
return -np.inf
177177
else:
178178
if fill_value_typ is None:
179-
return tslibs.iNaT
179+
return iNaT
180180
else:
181181
if fill_value_typ == "+inf":
182182
# need the max int here
183183
return _int64_max
184184
else:
185-
return tslibs.iNaT
185+
return iNaT
186186

187187

188188
def _maybe_get_mask(
189189
values: np.ndarray, skipna: bool, mask: Optional[np.ndarray]
190190
) -> Optional[np.ndarray]:
191-
""" This function will compute a mask iff it is necessary. Otherwise,
191+
"""
192+
Compute a mask if and only if necessary.
193+
194+
This function will compute a mask iff it is necessary. Otherwise,
192195
return the provided mask (potentially None) when a mask does not need to be
193196
computed.
194197
@@ -214,7 +217,6 @@ def _maybe_get_mask(
214217
Returns
215218
-------
216219
Optional[np.ndarray]
217-
218220
"""
219221

220222
if mask is None:
@@ -346,7 +348,7 @@ def _wrap_results(result, dtype, fill_value=None):
346348
assert not isna(fill_value), "Expected non-null fill_value"
347349
if result == fill_value:
348350
result = np.nan
349-
result = tslibs.Timestamp(result, tz=tz)
351+
result = Timestamp(result, tz=tz)
350352
else:
351353
result = result.view(dtype)
352354
elif is_timedelta64_dtype(dtype):
@@ -358,21 +360,22 @@ def _wrap_results(result, dtype, fill_value=None):
358360
if np.fabs(result) > _int64_max:
359361
raise ValueError("overflow in timedelta operation")
360362

361-
result = tslibs.Timedelta(result, unit="ns")
363+
result = Timedelta(result, unit="ns")
362364
else:
363365
result = result.astype("m8[ns]").view(dtype)
364366

365367
return result
366368

367369

368-
def _na_for_min_count(values, axis):
369-
"""Return the missing value for `values`
370+
def _na_for_min_count(values, axis: Optional[int]):
371+
"""
372+
Return the missing value for `values`.
370373
371374
Parameters
372375
----------
373376
values : ndarray
374377
axis : int or None
375-
axis for the reduction
378+
axis for the reduction, required if values.ndim > 1.
376379
377380
Returns
378381
-------
@@ -388,13 +391,14 @@ def _na_for_min_count(values, axis):
388391
if values.ndim == 1:
389392
return fill_value
390393
else:
394+
assert axis is not None # assertion to make mypy happy
391395
result_shape = values.shape[:axis] + values.shape[axis + 1 :]
392396
result = np.empty(result_shape, dtype=values.dtype)
393397
result.fill(fill_value)
394398
return result
395399

396400

397-
def nanany(values, axis=None, skipna=True, mask=None):
401+
def nanany(values, axis=None, skipna: bool = True, mask=None):
398402
"""
399403
Check if any elements along an axis evaluate to True.
400404
@@ -426,7 +430,7 @@ def nanany(values, axis=None, skipna=True, mask=None):
426430
return values.any(axis)
427431

428432

429-
def nanall(values, axis=None, skipna=True, mask=None):
433+
def nanall(values, axis=None, skipna: bool = True, mask=None):
430434
"""
431435
Check if all elements along an axis evaluate to True.
432436
@@ -1195,7 +1199,7 @@ def _maybe_null_out(
11951199
else:
11961200
# GH12941, use None to auto cast null
11971201
result[null_mask] = None
1198-
elif result is not tslibs.NaT:
1202+
elif result is not NaT:
11991203
if mask is not None:
12001204
null_mask = mask.size - mask.sum()
12011205
else:

pandas/io/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,11 @@ def __iter__(self) -> "MMapWrapper":
569569
return self
570570

571571
def __next__(self) -> str:
572-
newline = self.mmap.readline()
572+
newbytes = self.mmap.readline()
573573

574574
# readline returns bytes, not str, but Python's CSV reader
575575
# expects str, so convert the output to str before continuing
576-
newline = newline.decode("utf-8")
576+
newline = newbytes.decode("utf-8")
577577

578578
# mmap doesn't raise if reading past the allocated
579579
# data but instead returns an empty string, so raise

pandas/io/excel/_odfreader.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010

1111
class _ODFReader(_BaseExcelReader):
12-
"""Read tables out of OpenDocument formatted files
12+
"""
13+
Read tables out of OpenDocument formatted files.
1314
1415
Parameters
1516
----------

0 commit comments

Comments
 (0)