17
17
Sequence ,
18
18
Set ,
19
19
Tuple ,
20
+ Type ,
20
21
TypeVar ,
21
22
Union ,
22
23
cast ,
47
48
Dtype ,
48
49
DtypeObj ,
49
50
Shape ,
51
+ T ,
50
52
final ,
51
53
)
52
54
from pandas .compat .numpy import function as nv
161
163
if TYPE_CHECKING :
162
164
from pandas import (
163
165
CategoricalIndex ,
166
+ DataFrame ,
164
167
IntervalIndex ,
165
168
MultiIndex ,
166
169
RangeIndex ,
@@ -278,16 +281,22 @@ class Index(IndexOpsMixin, PandasObject):
278
281
# for why we need to wrap these instead of making them class attributes
279
282
# Moreover, cython will choose the appropriate-dtyped sub-function
280
283
# given the dtypes of the passed arguments
281
- def _left_indexer_unique (self , left , right ) :
284
+ def _left_indexer_unique (self , left : np . ndarray , right : np . ndarray ) -> np . ndarray :
282
285
return libjoin .left_join_indexer_unique (left , right )
283
286
284
- def _left_indexer (self , left , right ):
287
+ def _left_indexer (
288
+ self , left : np .ndarray , right : np .ndarray
289
+ ) -> Tuple [np .ndarray , np .ndarray , np .ndarray ]:
285
290
return libjoin .left_join_indexer (left , right )
286
291
287
- def _inner_indexer (self , left , right ):
292
+ def _inner_indexer (
293
+ self , left : np .ndarray , right : np .ndarray
294
+ ) -> Tuple [np .ndarray , np .ndarray , np .ndarray ]:
288
295
return libjoin .inner_join_indexer (left , right )
289
296
290
- def _outer_indexer (self , left , right ):
297
+ def _outer_indexer (
298
+ self , left : np .ndarray , right : np .ndarray
299
+ ) -> Tuple [np .ndarray , np .ndarray , np .ndarray ]:
291
300
return libjoin .outer_join_indexer (left , right )
292
301
293
302
_typ = "index"
@@ -548,7 +557,7 @@ def asi8(self):
548
557
return None
549
558
550
559
@classmethod
551
- def _simple_new (cls , values , name : Hashable = None ):
560
+ def _simple_new (cls : Type [ _IndexT ] , values , name : Hashable = None ) -> _IndexT :
552
561
"""
553
562
We require that we have a dtype compat for the values. If we are passed
554
563
a non-dtype compat, then coerce using the constructor.
@@ -571,11 +580,11 @@ def _simple_new(cls, values, name: Hashable = None):
571
580
return result
572
581
573
582
@cache_readonly
574
- def _constructor (self ) :
583
+ def _constructor (self : _IndexT ) -> Type [ _IndexT ] :
575
584
return type (self )
576
585
577
586
@final
578
- def _maybe_check_unique (self ):
587
+ def _maybe_check_unique (self ) -> None :
579
588
"""
580
589
Check that an Index has no duplicates.
581
590
@@ -626,13 +635,13 @@ def _format_duplicate_message(self):
626
635
# Index Internals Methods
627
636
628
637
@final
629
- def _get_attributes_dict (self ):
638
+ def _get_attributes_dict (self ) -> Dict [ str_t , Any ] :
630
639
"""
631
640
Return an attributes dict for my class.
632
641
"""
633
642
return {k : getattr (self , k , None ) for k in self ._attributes }
634
643
635
- def _shallow_copy (self , values , name : Hashable = no_default ):
644
+ def _shallow_copy (self : _IndexT , values , name : Hashable = no_default ) -> _IndexT :
636
645
"""
637
646
Create a new Index with the same class as the caller, don't copy the
638
647
data, use the same object attributes with passed in attributes taking
@@ -706,11 +715,11 @@ def _reset_identity(self) -> None:
706
715
self ._id = _Identity (object ())
707
716
708
717
@final
709
- def _cleanup (self ):
718
+ def _cleanup (self ) -> None :
710
719
self ._engine .clear_mapping ()
711
720
712
721
@cache_readonly
713
- def _engine (self ):
722
+ def _engine (self ) -> libindex . ObjectEngine :
714
723
# property, for now, slow to look up
715
724
716
725
# to avoid a reference cycle, bind `target_values` to a local variable, so
@@ -1260,7 +1269,7 @@ def to_flat_index(self):
1260
1269
"""
1261
1270
return self
1262
1271
1263
- def to_series (self , index = None , name = None ):
1272
+ def to_series (self , index = None , name : Hashable = None ) -> Series :
1264
1273
"""
1265
1274
Create a Series with both index and values equal to the index keys.
1266
1275
@@ -1323,7 +1332,7 @@ def to_series(self, index=None, name=None):
1323
1332
1324
1333
return Series (self ._values .copy (), index = index , name = name )
1325
1334
1326
- def to_frame (self , index : bool = True , name = None ):
1335
+ def to_frame (self , index : bool = True , name = None ) -> DataFrame :
1327
1336
"""
1328
1337
Create a DataFrame with a column containing the Index.
1329
1338
@@ -1438,10 +1447,10 @@ def _validate_names(
1438
1447
1439
1448
return new_names
1440
1449
1441
- def _get_names (self ):
1450
+ def _get_names (self ) -> FrozenList :
1442
1451
return FrozenList ((self .name ,))
1443
1452
1444
- def _set_names (self , values , level = None ):
1453
+ def _set_names (self , values , level = None ) -> None :
1445
1454
"""
1446
1455
Set new names on index. Each name has to be a hashable type.
1447
1456
@@ -1642,14 +1651,14 @@ def nlevels(self) -> int:
1642
1651
"""
1643
1652
return 1
1644
1653
1645
- def _sort_levels_monotonic (self ) :
1654
+ def _sort_levels_monotonic (self : _IndexT ) -> _IndexT :
1646
1655
"""
1647
1656
Compat with MultiIndex.
1648
1657
"""
1649
1658
return self
1650
1659
1651
1660
@final
1652
- def _validate_index_level (self , level ):
1661
+ def _validate_index_level (self , level ) -> None :
1653
1662
"""
1654
1663
Validate index level.
1655
1664
@@ -2386,7 +2395,7 @@ def hasnans(self) -> bool:
2386
2395
return False
2387
2396
2388
2397
@final
2389
- def isna (self ):
2398
+ def isna (self ) -> np . ndarray :
2390
2399
"""
2391
2400
Detect missing values.
2392
2401
@@ -2444,7 +2453,7 @@ def isna(self):
2444
2453
isnull = isna
2445
2454
2446
2455
@final
2447
- def notna (self ):
2456
+ def notna (self ) -> np . ndarray :
2448
2457
"""
2449
2458
Detect existing (non-missing) values.
2450
2459
@@ -2522,7 +2531,7 @@ def fillna(self, value=None, downcast=None):
2522
2531
return Index (result , name = self .name )
2523
2532
return self ._view ()
2524
2533
2525
- def dropna (self , how = "any" ):
2534
+ def dropna (self : _IndexT , how : str_t = "any" ) -> _IndexT :
2526
2535
"""
2527
2536
Return Index without NA/NaN values.
2528
2537
@@ -2547,20 +2556,21 @@ def dropna(self, how="any"):
2547
2556
# --------------------------------------------------------------------
2548
2557
# Uniqueness Methods
2549
2558
2550
- def unique (self , level = None ):
2559
+ def unique (self : _IndexT , level : Optional [ Hashable ] = None ) -> _IndexT :
2551
2560
"""
2552
2561
Return unique values in the index.
2553
2562
2554
2563
Unique values are returned in order of appearance, this does NOT sort.
2555
2564
2556
2565
Parameters
2557
2566
----------
2558
- level : int or str , optional, default None
2567
+ level : int or hashable , optional
2559
2568
Only return values from specified level (for MultiIndex).
2569
+ If int, gets the level by integer position, else by level name.
2560
2570
2561
2571
Returns
2562
2572
-------
2563
- Index without duplicates
2573
+ Index
2564
2574
2565
2575
See Also
2566
2576
--------
@@ -2577,7 +2587,7 @@ def unique(self, level=None):
2577
2587
return self ._shallow_copy (result )
2578
2588
2579
2589
@final
2580
- def drop_duplicates (self , keep = "first" ):
2590
+ def drop_duplicates (self : _IndexT , keep : Union [ str_t , bool ] = "first" ) -> _IndexT :
2581
2591
"""
2582
2592
Return Index with duplicate values removed.
2583
2593
@@ -2628,7 +2638,7 @@ def drop_duplicates(self, keep="first"):
2628
2638
2629
2639
return super ().drop_duplicates (keep = keep )
2630
2640
2631
- def duplicated (self , keep = "first" ):
2641
+ def duplicated (self , keep : Union [ str_t , bool ] = "first" ) -> np . ndarray :
2632
2642
"""
2633
2643
Indicate duplicate index values.
2634
2644
@@ -3214,12 +3224,12 @@ def symmetric_difference(self, other, result_name=None, sort=None):
3214
3224
return Index (the_diff , name = result_name )
3215
3225
3216
3226
@final
3217
- def _assert_can_do_setop (self , other ):
3227
+ def _assert_can_do_setop (self , other ) -> bool :
3218
3228
if not is_list_like (other ):
3219
3229
raise TypeError ("Input must be Index or array-like" )
3220
3230
return True
3221
3231
3222
- def _convert_can_do_setop (self , other ):
3232
+ def _convert_can_do_setop (self , other ) -> Tuple [ Index , Hashable ] :
3223
3233
if not isinstance (other , Index ):
3224
3234
other = Index (other , name = self .name )
3225
3235
result_name = self .name
@@ -3402,7 +3412,7 @@ def _get_indexer(
3402
3412
return ensure_platform_int (indexer )
3403
3413
3404
3414
@final
3405
- def _check_indexing_method (self , method ) :
3415
+ def _check_indexing_method (self , method : Optional [ str_t ]) -> None :
3406
3416
"""
3407
3417
Raise if we have a get_indexer `method` that is not supported or valid.
3408
3418
"""
@@ -3420,7 +3430,9 @@ def _check_indexing_method(self, method):
3420
3430
3421
3431
raise ValueError ("Invalid fill method" )
3422
3432
3423
- def _convert_tolerance (self , tolerance , target ):
3433
+ def _convert_tolerance (
3434
+ self , tolerance , target : Union [np .ndarray , Index ]
3435
+ ) -> np .ndarray :
3424
3436
# override this method on subclasses
3425
3437
tolerance = np .asarray (tolerance )
3426
3438
if target .size != tolerance .size and tolerance .size > 1 :
@@ -3523,7 +3535,7 @@ def _filter_indexer_tolerance(
3523
3535
# --------------------------------------------------------------------
3524
3536
# Indexer Conversion Methods
3525
3537
3526
- def _get_partial_string_timestamp_match_key (self , key ) :
3538
+ def _get_partial_string_timestamp_match_key (self , key : T ) -> T :
3527
3539
"""
3528
3540
Translate any partial string timestamp matches in key, returning the
3529
3541
new key.
@@ -3534,7 +3546,7 @@ def _get_partial_string_timestamp_match_key(self, key):
3534
3546
return key
3535
3547
3536
3548
@final
3537
- def _validate_positional_slice (self , key : slice ):
3549
+ def _validate_positional_slice (self , key : slice ) -> None :
3538
3550
"""
3539
3551
For positional indexing, a slice must have either int or None
3540
3552
for each of start, stop, and step.
@@ -3635,7 +3647,7 @@ def _convert_listlike_indexer(self, keyarr):
3635
3647
indexer = self ._convert_list_indexer (keyarr )
3636
3648
return indexer , keyarr
3637
3649
3638
- def _convert_arr_indexer (self , keyarr ):
3650
+ def _convert_arr_indexer (self , keyarr ) -> np . ndarray :
3639
3651
"""
3640
3652
Convert an array-like indexer to the appropriate dtype.
3641
3653
@@ -3680,13 +3692,13 @@ def _invalid_indexer(self, form: str_t, key) -> TypeError:
3680
3692
# Reindex Methods
3681
3693
3682
3694
@final
3683
- def _can_reindex (self , indexer ) :
3695
+ def _validate_can_reindex (self , indexer : np . ndarray ) -> None :
3684
3696
"""
3685
3697
Check if we are allowing reindexing with this particular indexer.
3686
3698
3687
3699
Parameters
3688
3700
----------
3689
- indexer : an integer indexer
3701
+ indexer : an integer ndarray
3690
3702
3691
3703
Raises
3692
3704
------
@@ -6209,7 +6221,7 @@ def trim_front(strings: List[str]) -> List[str]:
6209
6221
return strings
6210
6222
6211
6223
6212
- def _validate_join_method (method : str ):
6224
+ def _validate_join_method (method : str ) -> None :
6213
6225
if method not in ["left" , "right" , "inner" , "outer" ]:
6214
6226
raise ValueError (f"do not recognize join method { method } " )
6215
6227
@@ -6421,7 +6433,7 @@ def get_unanimous_names(*indexes: Index) -> Tuple[Hashable, ...]:
6421
6433
return names
6422
6434
6423
6435
6424
- def unpack_nested_dtype (other : Index ) -> Index :
6436
+ def unpack_nested_dtype (other : _IndexT ) -> _IndexT :
6425
6437
"""
6426
6438
When checking if our dtype is comparable with another, we need
6427
6439
to unpack CategoricalDtype to look at its categories.dtype.
0 commit comments