Skip to content

CLN: Assorted cleanups #28563

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 8 commits into from
Sep 23, 2019
9 changes: 3 additions & 6 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
is_period_dtype,
is_scalar,
is_signed_integer_dtype,
is_sparse,
is_timedelta64_dtype,
is_unsigned_integer_dtype,
needs_i8_conversion,
Expand Down Expand Up @@ -743,7 +742,7 @@ def value_counts(

else:

if is_extension_array_dtype(values) or is_sparse(values):
if is_extension_array_dtype(values):
Copy link
Member

Choose a reason for hiding this comment

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

For everyone's edification: why can we remove this check?

Copy link
Member Author

Choose a reason for hiding this comment

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

is_sparse is a subset of is_extension_array_dtype


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

if is_sparse(arr):
arr = arr.to_dense()
elif isinstance(arr, (ABCIndexClass, ABCSeries)):
if isinstance(arr, (ABCIndexClass, ABCSeries)):
arr = arr._values

arr = np.asarray(arr)
Expand Down
22 changes: 16 additions & 6 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
import numpy.ma as ma

from pandas._libs import lib, tslibs
from pandas._libs import lib
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime

from pandas.core.dtypes.cast import (
Expand All @@ -36,7 +36,7 @@
is_timedelta64_ns_dtype,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import ExtensionDtype, registry
from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype, registry
from pandas.core.dtypes.generic import (
ABCExtensionArray,
ABCIndexClass,
Expand Down Expand Up @@ -275,7 +275,7 @@ def array(
if inferred_dtype == "period":
try:
return period_array(data, copy=copy)
except tslibs.IncompatibleFrequency:
except IncompatibleFrequency:
# We may have a mixture of frequencies.
# We choose to return an ndarray, rather than raising.
pass
Expand Down Expand Up @@ -365,7 +365,9 @@ def extract_array(obj, extract_numpy=False):
return obj


def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False):
def sanitize_array(
data, index, dtype=None, copy: bool = False, raise_cast_failure: bool = False
):
"""
Sanitize input data to an ndarray, copy if specified, coerce to the
dtype if specified.
Expand Down Expand Up @@ -486,13 +488,19 @@ def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False
return subarr


def _try_cast(arr, dtype, copy, raise_cast_failure):
def _try_cast(
arr,
dtype: Optional[Union[np.dtype, "ExtensionDtype"]],
copy: bool,
raise_cast_failure: bool,
):
"""
Convert input to numpy ndarray and optionally cast to a given dtype.

Parameters
----------
arr : array-like
arr : ndarray, list, tuple, iterator (catchall)
Excludes: ExtensionArray, Series, Index.
dtype : np.dtype, ExtensionDtype or None
copy : bool
If False, don't copy the data if not needed.
Expand Down Expand Up @@ -528,11 +536,13 @@ def _try_cast(arr, dtype, copy, raise_cast_failure):
if is_categorical_dtype(dtype):
# We *do* allow casting to categorical, since we know
# that Categorical is the only array type for 'category'.
dtype = cast(CategoricalDtype, dtype)
subarr = dtype.construct_array_type()(
arr, dtype.categories, ordered=dtype._ordered
)
elif is_extension_array_dtype(dtype):
# create an extension array from its dtype
dtype = cast(ExtensionDtype, dtype)
Copy link
Member

Choose a reason for hiding this comment

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

For everyone's edification: why are we doing this? Same for the similar cast a few lines above.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is to keep mypy from complaining

array_type = dtype.construct_array_type()._from_sequence
subarr = array_type(arr, dtype=dtype, copy=copy)
elif dtype is not None and raise_cast_failure:
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,9 +1311,8 @@ def construct_1d_ndarray_preserving_na(values, dtype=None, copy=False):
>>> np.array([1.0, 2.0, None], dtype='str')
array(['1.0', '2.0', 'None'], dtype='<U4')
>>> construct_1d_ndarray_preserving_na([1.0, 2.0, None], dtype='str')
>>> construct_1d_ndarray_preserving_na([1.0, 2.0, None], dtype=np.dtype('str'))
array(['1.0', '2.0', None], dtype=object)
"""
subarr = np.array(values, dtype=dtype, copy=copy)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ def is_dtype_equal(source, target):


def is_any_int_dtype(arr_or_dtype) -> bool:
"""Check whether the provided array or dtype is of an integer dtype.
"""
Check whether the provided array or dtype is of an integer dtype.

In this function, timedelta64 instances are also considered "any-integer"
type objects and will return True.
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,8 @@ def _can_hold_na(self):
return True

def _maybe_coerce_values(self, values):
"""Input validation for values passed to __init__. Ensure that
"""
Input validation for values passed to __init__. Ensure that
we have datetime64ns, coercing if necessary.

Parameters
Expand Down