1
1
"""Sparse Dtype"""
2
2
3
3
import re
4
- from typing import Any , Tuple
4
+ from typing import TYPE_CHECKING , Any , Tuple , Type
5
5
6
6
import numpy as np
7
7
19
19
from pandas .core .dtypes .dtypes import register_extension_dtype
20
20
from pandas .core .dtypes .missing import isna , na_value_for_dtype
21
21
22
+ if TYPE_CHECKING :
23
+ from pandas .core .arrays .sparse .array import SparseArray # noqa: F401
24
+
22
25
23
26
@register_extension_dtype
24
27
class SparseDtype (ExtensionDtype ):
@@ -137,11 +140,11 @@ def _is_na_fill_value(self):
137
140
return isna (self .fill_value )
138
141
139
142
@property
140
- def _is_numeric (self ):
143
+ def _is_numeric (self ) -> bool :
141
144
return not is_object_dtype (self .subtype )
142
145
143
146
@property
144
- def _is_boolean (self ):
147
+ def _is_boolean (self ) -> bool :
145
148
return is_bool_dtype (self .subtype )
146
149
147
150
@property
@@ -167,20 +170,20 @@ def __repr__(self) -> str:
167
170
return self .name
168
171
169
172
@classmethod
170
- def construct_array_type (cls ):
173
+ def construct_array_type (cls ) -> Type [ "SparseArray" ] :
171
174
"""
172
175
Return the array type associated with this dtype.
173
176
174
177
Returns
175
178
-------
176
179
type
177
180
"""
178
- from pandas .core .arrays .sparse .array import SparseArray
181
+ from pandas .core .arrays .sparse .array import SparseArray # noqa: F811
179
182
180
183
return SparseArray
181
184
182
185
@classmethod
183
- def construct_from_string (cls , string ) :
186
+ def construct_from_string (cls , string : str ) -> "SparseDtype" :
184
187
"""
185
188
Construct a SparseDtype from a string form.
186
189
@@ -266,7 +269,7 @@ def _parse_subtype(dtype: str) -> Tuple[str, bool]:
266
269
return subtype , has_fill_value
267
270
268
271
@classmethod
269
- def is_dtype (cls , dtype ) :
272
+ def is_dtype (cls , dtype : object ) -> bool :
270
273
dtype = getattr (dtype , "dtype" , dtype )
271
274
if isinstance (dtype , str ) and dtype .startswith ("Sparse" ):
272
275
sub_type , _ = cls ._parse_subtype (dtype )
0 commit comments