Skip to content

TYP: pandas/core/arrays/sparse/dtype.py #31349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Sparse Dtype"""

import re
from typing import Any, Tuple
from typing import TYPE_CHECKING, Any, Tuple, Type

import numpy as np

Expand All @@ -19,6 +19,9 @@
from pandas.core.dtypes.dtypes import register_extension_dtype
from pandas.core.dtypes.missing import isna, na_value_for_dtype

if TYPE_CHECKING:
from pandas.core.arrays.sparse.array import SparseArray # noqa: F401


@register_extension_dtype
class SparseDtype(ExtensionDtype):
Expand Down Expand Up @@ -137,11 +140,11 @@ def _is_na_fill_value(self):
return isna(self.fill_value)

@property
def _is_numeric(self):
def _is_numeric(self) -> bool:
return not is_object_dtype(self.subtype)

@property
def _is_boolean(self):
def _is_boolean(self) -> bool:
return is_bool_dtype(self.subtype)

@property
Expand All @@ -167,20 +170,20 @@ def __repr__(self) -> str:
return self.name

@classmethod
def construct_array_type(cls):
def construct_array_type(cls) -> Type["SparseArray"]:
"""
Return the array type associated with this dtype.

Returns
-------
type
"""
from pandas.core.arrays.sparse.array import SparseArray
from pandas.core.arrays.sparse.array import SparseArray # noqa: F811

return SparseArray

@classmethod
def construct_from_string(cls, string):
def construct_from_string(cls, string: str) -> "SparseDtype":
"""
Construct a SparseDtype from a string form.

Expand Down Expand Up @@ -266,7 +269,7 @@ def _parse_subtype(dtype: str) -> Tuple[str, bool]:
return subtype, has_fill_value

@classmethod
def is_dtype(cls, dtype):
def is_dtype(cls, dtype: object) -> bool:
dtype = getattr(dtype, "dtype", dtype)
if isinstance(dtype, str) and dtype.startswith("Sparse"):
sub_type, _ = cls._parse_subtype(dtype)
Expand Down