Skip to content

Commit c8f76d5

Browse files
jbrockmendelproost
authored andcommitted
TYP: annotations in core.indexes (pandas-dev#29656)
1 parent db8e5c6 commit c8f76d5

15 files changed

+94
-88
lines changed

pandas/core/indexes/api.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import textwrap
2+
from typing import List, Set
23
import warnings
34

45
from pandas._libs import NaT, lib
@@ -64,17 +65,18 @@
6465
]
6566

6667

67-
def get_objs_combined_axis(objs, intersect=False, axis=0, sort=True):
68+
def get_objs_combined_axis(
69+
objs, intersect: bool = False, axis=0, sort: bool = True
70+
) -> Index:
6871
"""
6972
Extract combined index: return intersection or union (depending on the
7073
value of "intersect") of indexes on given axis, or None if all objects
7174
lack indexes (e.g. they are numpy arrays).
7275
7376
Parameters
7477
----------
75-
objs : list of objects
76-
Each object will only be considered if it has a _get_axis
77-
attribute.
78+
objs : list
79+
Series or DataFrame objects, may be mix of the two.
7880
intersect : bool, default False
7981
If True, calculate the intersection between indexes. Otherwise,
8082
calculate the union.
@@ -87,26 +89,27 @@ def get_objs_combined_axis(objs, intersect=False, axis=0, sort=True):
8789
-------
8890
Index
8991
"""
90-
obs_idxes = [obj._get_axis(axis) for obj in objs if hasattr(obj, "_get_axis")]
91-
if obs_idxes:
92-
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort)
92+
obs_idxes = [obj._get_axis(axis) for obj in objs]
93+
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort)
9394

9495

95-
def _get_distinct_objs(objs):
96+
def _get_distinct_objs(objs: List[Index]) -> List[Index]:
9697
"""
9798
Return a list with distinct elements of "objs" (different ids).
9899
Preserves order.
99100
"""
100-
ids = set()
101+
ids: Set[int] = set()
101102
res = []
102103
for obj in objs:
103-
if not id(obj) in ids:
104+
if id(obj) not in ids:
104105
ids.add(id(obj))
105106
res.append(obj)
106107
return res
107108

108109

109-
def _get_combined_index(indexes, intersect=False, sort=False):
110+
def _get_combined_index(
111+
indexes: List[Index], intersect: bool = False, sort: bool = False
112+
) -> Index:
110113
"""
111114
Return the union or intersection of indexes.
112115
@@ -147,7 +150,7 @@ def _get_combined_index(indexes, intersect=False, sort=False):
147150
return index
148151

149152

150-
def union_indexes(indexes, sort=True):
153+
def union_indexes(indexes, sort=True) -> Index:
151154
"""
152155
Return the union of indexes.
153156
@@ -173,7 +176,7 @@ def union_indexes(indexes, sort=True):
173176

174177
indexes, kind = _sanitize_and_check(indexes)
175178

176-
def _unique_indices(inds):
179+
def _unique_indices(inds) -> Index:
177180
"""
178181
Convert indexes to lists and concatenate them, removing duplicates.
179182

pandas/core/indexes/base.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ def _get_grouper_for_level(self, mapper, level=None):
16501650
# Introspection Methods
16511651

16521652
@property
1653-
def is_monotonic(self):
1653+
def is_monotonic(self) -> bool:
16541654
"""
16551655
Alias for is_monotonic_increasing.
16561656
"""
@@ -1691,7 +1691,7 @@ def is_monotonic_decreasing(self) -> bool:
16911691
return self._engine.is_monotonic_decreasing
16921692

16931693
@property
1694-
def _is_strictly_monotonic_increasing(self):
1694+
def _is_strictly_monotonic_increasing(self) -> bool:
16951695
"""
16961696
Return if the index is strictly monotonic increasing
16971697
(only increasing) values.
@@ -1708,7 +1708,7 @@ def _is_strictly_monotonic_increasing(self):
17081708
return self.is_unique and self.is_monotonic_increasing
17091709

17101710
@property
1711-
def _is_strictly_monotonic_decreasing(self):
1711+
def _is_strictly_monotonic_decreasing(self) -> bool:
17121712
"""
17131713
Return if the index is strictly monotonic decreasing
17141714
(only decreasing) values.
@@ -1725,7 +1725,7 @@ def _is_strictly_monotonic_decreasing(self):
17251725
return self.is_unique and self.is_monotonic_decreasing
17261726

17271727
@cache_readonly
1728-
def is_unique(self):
1728+
def is_unique(self) -> bool:
17291729
"""
17301730
Return if the index has unique values.
17311731
"""
@@ -1735,22 +1735,22 @@ def is_unique(self):
17351735
def has_duplicates(self) -> bool:
17361736
return not self.is_unique
17371737

1738-
def is_boolean(self):
1738+
def is_boolean(self) -> bool:
17391739
return self.inferred_type in ["boolean"]
17401740

1741-
def is_integer(self):
1741+
def is_integer(self) -> bool:
17421742
return self.inferred_type in ["integer"]
17431743

1744-
def is_floating(self):
1744+
def is_floating(self) -> bool:
17451745
return self.inferred_type in ["floating", "mixed-integer-float", "integer-na"]
17461746

1747-
def is_numeric(self):
1747+
def is_numeric(self) -> bool:
17481748
return self.inferred_type in ["integer", "floating"]
17491749

1750-
def is_object(self):
1750+
def is_object(self) -> bool:
17511751
return is_object_dtype(self.dtype)
17521752

1753-
def is_categorical(self):
1753+
def is_categorical(self) -> bool:
17541754
"""
17551755
Check if the Index holds categorical data.
17561756
@@ -1786,10 +1786,10 @@ def is_categorical(self):
17861786
"""
17871787
return self.inferred_type in ["categorical"]
17881788

1789-
def is_interval(self):
1789+
def is_interval(self) -> bool:
17901790
return self.inferred_type in ["interval"]
17911791

1792-
def is_mixed(self):
1792+
def is_mixed(self) -> bool:
17931793
return self.inferred_type in ["mixed"]
17941794

17951795
def holds_integer(self):
@@ -1868,7 +1868,7 @@ def _isnan(self):
18681868
@cache_readonly
18691869
def _nan_idxs(self):
18701870
if self._can_hold_na:
1871-
w, = self._isnan.nonzero()
1871+
w = self._isnan.nonzero()[0]
18721872
return w
18731873
else:
18741874
return np.array([], dtype=np.int64)
@@ -4086,13 +4086,13 @@ def _assert_can_do_op(self, value):
40864086
msg = "'value' must be a scalar, passed: {0}"
40874087
raise TypeError(msg.format(type(value).__name__))
40884088

4089-
def _is_memory_usage_qualified(self):
4089+
def _is_memory_usage_qualified(self) -> bool:
40904090
"""
40914091
Return a boolean if we need a qualified .info display.
40924092
"""
40934093
return self.is_object()
40944094

4095-
def is_type_compatible(self, kind):
4095+
def is_type_compatible(self, kind) -> bool:
40964096
"""
40974097
Whether the index type is compatible with the provided type.
40984098
"""
@@ -4131,14 +4131,14 @@ def is_type_compatible(self, kind):
41314131
"""
41324132

41334133
@Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
4134-
def __contains__(self, key):
4134+
def __contains__(self, key) -> bool:
41354135
hash(key)
41364136
try:
41374137
return key in self._engine
41384138
except (OverflowError, TypeError, ValueError):
41394139
return False
41404140

4141-
def contains(self, key):
4141+
def contains(self, key) -> bool:
41424142
"""
41434143
Return a boolean indicating whether the provided key is in the index.
41444144
@@ -4199,7 +4199,7 @@ def __getitem__(self, key):
41994199
else:
42004200
return result
42014201

4202-
def _can_hold_identifiers_and_holds_name(self, name):
4202+
def _can_hold_identifiers_and_holds_name(self, name) -> bool:
42034203
"""
42044204
Faster check for ``name in self`` when we know `name` is a Python
42054205
identifier (e.g. in NDFrame.__getattr__, which hits this to support
@@ -4290,7 +4290,7 @@ def putmask(self, mask, value):
42904290
# coerces to object
42914291
return self.astype(object).putmask(mask, value)
42924292

4293-
def equals(self, other):
4293+
def equals(self, other) -> bool:
42944294
"""
42954295
Determine if two Index objects contain the same elements.
42964296
@@ -4314,7 +4314,7 @@ def equals(self, other):
43144314
com.values_from_object(self), com.values_from_object(other)
43154315
)
43164316

4317-
def identical(self, other):
4317+
def identical(self, other) -> bool:
43184318
"""
43194319
Similar to equals, but check that other comparable attributes are
43204320
also equal.

pandas/core/indexes/category.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def _shallow_copy(self, values=None, dtype=None, **kwargs):
276276
dtype = self.dtype
277277
return super()._shallow_copy(values=values, dtype=dtype, **kwargs)
278278

279-
def _is_dtype_compat(self, other):
279+
def _is_dtype_compat(self, other) -> bool:
280280
"""
281281
*this is an internal non-public method*
282282
@@ -407,7 +407,7 @@ def _reverse_indexer(self):
407407
return self._data._reverse_indexer()
408408

409409
@Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
410-
def __contains__(self, key):
410+
def __contains__(self, key) -> bool:
411411
# if key is a NaN, check if any NaN is in self.
412412
if is_scalar(key) and isna(key):
413413
return self.hasnans
@@ -455,7 +455,7 @@ def _engine(self):
455455

456456
# introspection
457457
@cache_readonly
458-
def is_unique(self):
458+
def is_unique(self) -> bool:
459459
return self._engine.is_unique
460460

461461
@property

pandas/core/indexes/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def wrapper(self, other):
148148
return wrapper
149149

150150
@property
151-
def _ndarray_values(self):
151+
def _ndarray_values(self) -> np.ndarray:
152152
return self._data._ndarray_values
153153

154154
# ------------------------------------------------------------------------

pandas/core/indexes/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def tz(self, value):
410410
tzinfo = tz
411411

412412
@cache_readonly
413-
def _is_dates_only(self):
413+
def _is_dates_only(self) -> bool:
414414
"""Return a boolean if we are only dates (and don't have a timezone)"""
415415
from pandas.io.formats.format import _is_dates_only
416416

@@ -1237,7 +1237,7 @@ def searchsorted(self, value, side="left", sorter=None):
12371237

12381238
return self.values.searchsorted(value, side=side)
12391239

1240-
def is_type_compatible(self, typ):
1240+
def is_type_compatible(self, typ) -> bool:
12411241
return typ == self.inferred_type or typ == "datetime"
12421242

12431243
@property

pandas/core/indexes/interval.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def _engine(self):
343343
right = self._maybe_convert_i8(self.right)
344344
return IntervalTree(left, right, closed=self.closed)
345345

346-
def __contains__(self, key):
346+
def __contains__(self, key) -> bool:
347347
"""
348348
return a boolean if this key is IN the index
349349
We *only* accept an Interval
@@ -483,7 +483,7 @@ def _values(self):
483483
return self._data
484484

485485
@cache_readonly
486-
def _ndarray_values(self):
486+
def _ndarray_values(self) -> np.ndarray:
487487
return np.array(self._data)
488488

489489
def __array__(self, result=None):
@@ -529,7 +529,7 @@ def inferred_type(self) -> str:
529529
return "interval"
530530

531531
@Appender(Index.memory_usage.__doc__)
532-
def memory_usage(self, deep=False):
532+
def memory_usage(self, deep: bool = False) -> int:
533533
# we don't use an explicit engine
534534
# so return the bytes here
535535
return self.left.memory_usage(deep=deep) + self.right.memory_usage(deep=deep)
@@ -542,15 +542,15 @@ def mid(self):
542542
return self._data.mid
543543

544544
@cache_readonly
545-
def is_monotonic(self):
545+
def is_monotonic(self) -> bool:
546546
"""
547547
Return True if the IntervalIndex is monotonic increasing (only equal or
548548
increasing values), else False
549549
"""
550550
return self.is_monotonic_increasing
551551

552552
@cache_readonly
553-
def is_monotonic_increasing(self):
553+
def is_monotonic_increasing(self) -> bool:
554554
"""
555555
Return True if the IntervalIndex is monotonic increasing (only equal or
556556
increasing values), else False
@@ -1213,7 +1213,7 @@ def _format_space(self):
12131213
def argsort(self, *args, **kwargs):
12141214
return np.lexsort((self.right, self.left))
12151215

1216-
def equals(self, other):
1216+
def equals(self, other) -> bool:
12171217
"""
12181218
Determines if two IntervalIndex objects contain the same elements
12191219
"""
@@ -1374,7 +1374,7 @@ def is_all_dates(self) -> bool:
13741374
IntervalIndex._add_logical_methods_disabled()
13751375

13761376

1377-
def _is_valid_endpoint(endpoint):
1377+
def _is_valid_endpoint(endpoint) -> bool:
13781378
"""helper for interval_range to check if start/end are valid types"""
13791379
return any(
13801380
[
@@ -1386,7 +1386,7 @@ def _is_valid_endpoint(endpoint):
13861386
)
13871387

13881388

1389-
def _is_type_compatible(a, b):
1389+
def _is_type_compatible(a, b) -> bool:
13901390
"""helper for interval_range to check type compat of start/end/freq"""
13911391
is_ts_compat = lambda x: isinstance(x, (Timestamp, DateOffset))
13921392
is_td_compat = lambda x: isinstance(x, (Timedelta, DateOffset))

0 commit comments

Comments
 (0)