3
3
"""
4
4
import builtins
5
5
import textwrap
6
- from typing import Dict , FrozenSet , List , Optional
6
+ from typing import Dict , FrozenSet , List , Optional , Union
7
7
8
8
import numpy as np
9
9
@@ -110,7 +110,7 @@ def _freeze(self):
110
110
object .__setattr__ (self , "__frozen" , True )
111
111
112
112
# prevent adding any attribute via s.xxx.new_attribute = ...
113
- def __setattr__ (self , key , value ):
113
+ def __setattr__ (self , key : str , value ):
114
114
# _cache is used by a decorator
115
115
# We need to check both 1.) cls.__dict__ and 2.) getattr(self, key)
116
116
# because
@@ -239,7 +239,7 @@ def __getitem__(self, key):
239
239
raise KeyError (f"Column not found: { key } " )
240
240
return self ._gotitem (key , ndim = 1 )
241
241
242
- def _gotitem (self , key , ndim , subset = None ):
242
+ def _gotitem (self , key , ndim : int , subset = None ):
243
243
"""
244
244
sub-classes to define
245
245
return a sliced object
@@ -605,6 +605,11 @@ class IndexOpsMixin:
605
605
["tolist" ] # tolist is not deprecated, just suppressed in the __dir__
606
606
)
607
607
608
+ @property
609
+ def _values (self ) -> Union [ExtensionArray , np .ndarray ]:
610
+ # must be defined here as a property for mypy
611
+ raise AbstractMethodError (self )
612
+
608
613
def transpose (self , * args , ** kwargs ):
609
614
"""
610
615
Return the transpose, which is by definition self.
@@ -630,6 +635,10 @@ def shape(self):
630
635
"""
631
636
return self ._values .shape
632
637
638
+ def __len__ (self ) -> int :
639
+ # We need this defined here for mypy
640
+ raise AbstractMethodError (self )
641
+
633
642
@property
634
643
def ndim (self ) -> int :
635
644
"""
@@ -664,14 +673,14 @@ def item(self):
664
673
raise ValueError ("can only convert an array of size 1 to a Python scalar" )
665
674
666
675
@property
667
- def nbytes (self ):
676
+ def nbytes (self ) -> int :
668
677
"""
669
678
Return the number of bytes in the underlying data.
670
679
"""
671
680
return self ._values .nbytes
672
681
673
682
@property
674
- def size (self ):
683
+ def size (self ) -> int :
675
684
"""
676
685
Return the number of elements in the underlying data.
677
686
"""
@@ -1058,7 +1067,14 @@ def hasnans(self):
1058
1067
return bool (isna (self ).any ())
1059
1068
1060
1069
def _reduce (
1061
- self , op , name , axis = 0 , skipna = True , numeric_only = None , filter_type = None , ** kwds
1070
+ self ,
1071
+ op ,
1072
+ name : str ,
1073
+ axis = 0 ,
1074
+ skipna = True ,
1075
+ numeric_only = None ,
1076
+ filter_type = None ,
1077
+ ** kwds ,
1062
1078
):
1063
1079
""" perform the reduction type operation if we can """
1064
1080
func = getattr (self , name , None )
@@ -1254,7 +1270,7 @@ def unique(self):
1254
1270
1255
1271
return result
1256
1272
1257
- def nunique (self , dropna = True ):
1273
+ def nunique (self , dropna : bool = True ) -> int :
1258
1274
"""
1259
1275
Return number of unique elements in the object.
1260
1276
@@ -1295,7 +1311,7 @@ def nunique(self, dropna=True):
1295
1311
return n
1296
1312
1297
1313
@property
1298
- def is_unique (self ):
1314
+ def is_unique (self ) -> bool :
1299
1315
"""
1300
1316
Return boolean if values in the object are unique.
1301
1317
@@ -1306,7 +1322,7 @@ def is_unique(self):
1306
1322
return self .nunique (dropna = False ) == len (self )
1307
1323
1308
1324
@property
1309
- def is_monotonic (self ):
1325
+ def is_monotonic (self ) -> bool :
1310
1326
"""
1311
1327
Return boolean if values in the object are
1312
1328
monotonic_increasing.
@@ -1319,7 +1335,11 @@ def is_monotonic(self):
1319
1335
1320
1336
return Index (self ).is_monotonic
1321
1337
1322
- is_monotonic_increasing = is_monotonic
1338
+ @property
1339
+ def is_monotonic_increasing (self ) -> bool :
1340
+ """alias for is_monotonic"""
1341
+ # mypy complains if we alias directly
1342
+ return self .is_monotonic
1323
1343
1324
1344
@property
1325
1345
def is_monotonic_decreasing (self ) -> bool :
@@ -1472,7 +1492,7 @@ def factorize(self, sort=False, na_sentinel=-1):
1472
1492
1473
1493
@Substitution (klass = "Index" )
1474
1494
@Appender (_shared_docs ["searchsorted" ])
1475
- def searchsorted (self , value , side = "left" , sorter = None ):
1495
+ def searchsorted (self , value , side = "left" , sorter = None ) -> np . ndarray :
1476
1496
return algorithms .searchsorted (self ._values , value , side = side , sorter = sorter )
1477
1497
1478
1498
def drop_duplicates (self , keep = "first" , inplace = False ):
0 commit comments