Skip to content

Commit a1f9270

Browse files
jbrockmendelproost
authored andcommitted
CLN: Assorted cleanups (pandas-dev#28563)
1 parent 3b038af commit a1f9270

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

pandas/core/algorithms.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
is_period_dtype,
4040
is_scalar,
4141
is_signed_integer_dtype,
42-
is_sparse,
4342
is_timedelta64_dtype,
4443
is_unsigned_integer_dtype,
4544
needs_i8_conversion,
@@ -743,7 +742,7 @@ def value_counts(
743742

744743
else:
745744

746-
if is_extension_array_dtype(values) or is_sparse(values):
745+
if is_extension_array_dtype(values):
747746

748747
# handle Categorical and sparse,
749748
result = Series(values)._values.value_counts(dropna=dropna)
@@ -1623,7 +1622,7 @@ def take_nd(
16231622
out : ndarray or None, default None
16241623
Optional output array, must be appropriate type to hold input and
16251624
fill_value together, if indexer has any -1 value entries; call
1626-
_maybe_promote to determine this type for any fill_value
1625+
maybe_promote to determine this type for any fill_value
16271626
fill_value : any, default np.nan
16281627
Fill value to replace -1 values with
16291628
mask_info : tuple of (ndarray, boolean)
@@ -1644,9 +1643,7 @@ def take_nd(
16441643
if is_extension_array_dtype(arr):
16451644
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
16461645

1647-
if is_sparse(arr):
1648-
arr = arr.to_dense()
1649-
elif isinstance(arr, (ABCIndexClass, ABCSeries)):
1646+
if isinstance(arr, (ABCIndexClass, ABCSeries)):
16501647
arr = arr._values
16511648

16521649
arr = np.asarray(arr)

pandas/core/construction.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010
import numpy.ma as ma
1111

12-
from pandas._libs import lib, tslibs
12+
from pandas._libs import lib
1313
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime
1414

1515
from pandas.core.dtypes.cast import (
@@ -36,7 +36,7 @@
3636
is_timedelta64_ns_dtype,
3737
pandas_dtype,
3838
)
39-
from pandas.core.dtypes.dtypes import ExtensionDtype, registry
39+
from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype, registry
4040
from pandas.core.dtypes.generic import (
4141
ABCExtensionArray,
4242
ABCIndexClass,
@@ -275,7 +275,7 @@ def array(
275275
if inferred_dtype == "period":
276276
try:
277277
return period_array(data, copy=copy)
278-
except tslibs.IncompatibleFrequency:
278+
except IncompatibleFrequency:
279279
# We may have a mixture of frequencies.
280280
# We choose to return an ndarray, rather than raising.
281281
pass
@@ -365,7 +365,9 @@ def extract_array(obj, extract_numpy=False):
365365
return obj
366366

367367

368-
def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False):
368+
def sanitize_array(
369+
data, index, dtype=None, copy: bool = False, raise_cast_failure: bool = False
370+
):
369371
"""
370372
Sanitize input data to an ndarray, copy if specified, coerce to the
371373
dtype if specified.
@@ -486,13 +488,19 @@ def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False
486488
return subarr
487489

488490

489-
def _try_cast(arr, dtype, copy, raise_cast_failure):
491+
def _try_cast(
492+
arr,
493+
dtype: Optional[Union[np.dtype, "ExtensionDtype"]],
494+
copy: bool,
495+
raise_cast_failure: bool,
496+
):
490497
"""
491498
Convert input to numpy ndarray and optionally cast to a given dtype.
492499
493500
Parameters
494501
----------
495-
arr : array-like
502+
arr : ndarray, list, tuple, iterator (catchall)
503+
Excludes: ExtensionArray, Series, Index.
496504
dtype : np.dtype, ExtensionDtype or None
497505
copy : bool
498506
If False, don't copy the data if not needed.
@@ -528,11 +536,13 @@ def _try_cast(arr, dtype, copy, raise_cast_failure):
528536
if is_categorical_dtype(dtype):
529537
# We *do* allow casting to categorical, since we know
530538
# that Categorical is the only array type for 'category'.
539+
dtype = cast(CategoricalDtype, dtype)
531540
subarr = dtype.construct_array_type()(
532541
arr, dtype.categories, ordered=dtype._ordered
533542
)
534543
elif is_extension_array_dtype(dtype):
535544
# create an extension array from its dtype
545+
dtype = cast(ExtensionDtype, dtype)
536546
array_type = dtype.construct_array_type()._from_sequence
537547
subarr = array_type(arr, dtype=dtype, copy=copy)
538548
elif dtype is not None and raise_cast_failure:

pandas/core/dtypes/cast.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1311,9 +1311,8 @@ def construct_1d_ndarray_preserving_na(values, dtype=None, copy=False):
13111311
>>> np.array([1.0, 2.0, None], dtype='str')
13121312
array(['1.0', '2.0', 'None'], dtype='<U4')
13131313
1314-
>>> construct_1d_ndarray_preserving_na([1.0, 2.0, None], dtype='str')
1315-
1316-
1314+
>>> construct_1d_ndarray_preserving_na([1.0, 2.0, None], dtype=np.dtype('str'))
1315+
array(['1.0', '2.0', None], dtype=object)
13171316
"""
13181317
subarr = np.array(values, dtype=dtype, copy=copy)
13191318

pandas/core/dtypes/common.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,8 @@ def is_dtype_equal(source, target):
888888

889889

890890
def is_any_int_dtype(arr_or_dtype) -> bool:
891-
"""Check whether the provided array or dtype is of an integer dtype.
891+
"""
892+
Check whether the provided array or dtype is of an integer dtype.
892893
893894
In this function, timedelta64 instances are also considered "any-integer"
894895
type objects and will return True.

pandas/core/internals/blocks.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,8 @@ def _can_hold_na(self):
21282128
return True
21292129

21302130
def _maybe_coerce_values(self, values):
2131-
"""Input validation for values passed to __init__. Ensure that
2131+
"""
2132+
Input validation for values passed to __init__. Ensure that
21322133
we have datetime64ns, coercing if necessary.
21332134
21342135
Parameters

0 commit comments

Comments
 (0)