Skip to content

MNT Remove deprecated pandas.api.types.is_sparse #26287

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 2 commits into from
Apr 27, 2023
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
16 changes: 11 additions & 5 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,17 +595,17 @@ def _pandas_dtype_needs_early_conversion(pd_dtype):
# Check these early for pandas versions without extension dtypes
from pandas.api.types import (
is_bool_dtype,
is_sparse,
is_float_dtype,
is_integer_dtype,
)
from pandas import SparseDtype

if is_bool_dtype(pd_dtype):
# bool and extension booleans need early converstion because __array__
# converts mixed dtype dataframes into object dtypes
return True

if is_sparse(pd_dtype):
if isinstance(pd_dtype, SparseDtype):
# Sparse arrays will be converted later in `check_array`
return False

Expand All @@ -614,7 +614,7 @@ def _pandas_dtype_needs_early_conversion(pd_dtype):
except ImportError:
return False

if is_sparse(pd_dtype) or not is_extension_array_dtype(pd_dtype):
if isinstance(pd_dtype, SparseDtype) or not is_extension_array_dtype(pd_dtype):
# Sparse arrays will be converted later in `check_array`
# Only handle extension arrays for integer and floats
return False
Expand Down Expand Up @@ -769,7 +769,10 @@ def check_array(
# throw warning if columns are sparse. If all columns are sparse, then
# array.sparse exists and sparsity will be preserved (later).
with suppress(ImportError):
from pandas.api.types import is_sparse
from pandas import SparseDtype

def is_sparse(dtype):
return isinstance(dtype, SparseDtype)

if not hasattr(array, "sparse") and array.dtypes.apply(is_sparse).any():
warnings.warn(
Expand Down Expand Up @@ -847,7 +850,10 @@ def check_array(
# When all dataframe columns are sparse, convert to a sparse array
if hasattr(array, "sparse") and array.ndim > 1:
with suppress(ImportError):
from pandas.api.types import is_sparse
from pandas import SparseDtype # noqa: F811
Copy link
Member Author

@lesteve lesteve Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flake8 gives an error without the noqa:

sklearn/utils/validation.py:853:13: F811 redefinition of unused 'SparseDtype' from line 772

It may well be that the code above already defines SparseDtype in a if clause, I feel this is quite brittle to rely on this and I would rather silence flake8 in this case.


def is_sparse(dtype):
return isinstance(dtype, SparseDtype)

if array.dtypes.apply(is_sparse).all():
# DataFrame.sparse only supports `to_coo`
Expand Down