Skip to content

CLN: maybe_promote doesnt need to support EA dtypes #39760

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 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,12 @@ def take(arr, indices, axis: int = 0, allow_fill: bool = False, fill_value=None)


def _take_preprocess_indexer_and_fill_value(
arr, indexer, axis, out, fill_value, allow_fill
arr: np.ndarray,
indexer: Optional[np.ndarray],
axis: int,
out: Optional[np.ndarray],
fill_value,
allow_fill: bool,
):
mask_info = None

Expand Down Expand Up @@ -1783,7 +1788,9 @@ def take_nd(
return out


def take_2d_multi(arr, indexer, fill_value=np.nan):
def take_2d_multi(
arr: np.ndarray, indexer: np.ndarray, fill_value=np.nan
) -> np.ndarray:
"""
Specialized Cython take which sets NaN values in one pass.
"""
Expand Down
24 changes: 4 additions & 20 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
conversion,
iNaT,
ints_to_pydatetime,
tz_compare,
)
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar
from pandas.util._exceptions import find_stack_level
Expand Down Expand Up @@ -499,13 +498,13 @@ def ensure_dtype_can_hold_na(dtype: DtypeObj) -> DtypeObj:
return dtype


def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
def maybe_promote(dtype: np.dtype, fill_value=np.nan):
"""
Find the minimal dtype that can hold both the given dtype and fill_value.

Parameters
----------
dtype : np.dtype or ExtensionDtype
dtype : np.dtype
fill_value : scalar, default np.nan

Returns
Expand Down Expand Up @@ -567,19 +566,6 @@ def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
fill_value = np.timedelta64("NaT", "ns")
else:
fill_value = fv.to_timedelta64()
elif isinstance(dtype, DatetimeTZDtype):
if isna(fill_value):
fill_value = NaT
elif not isinstance(fill_value, datetime):
dtype = np.dtype(np.object_)
elif fill_value.tzinfo is None:
dtype = np.dtype(np.object_)
elif not tz_compare(fill_value.tzinfo, dtype.tz):
# TODO: sure we want to cast here?
dtype = np.dtype(np.object_)

elif is_extension_array_dtype(dtype) and isna(fill_value):
fill_value = dtype.na_value

elif is_float(fill_value):
if issubclass(dtype.type, np.bool_):
Expand Down Expand Up @@ -634,7 +620,7 @@ def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
if is_float_dtype(dtype) or is_complex_dtype(dtype):
fill_value = np.nan
elif is_integer_dtype(dtype):
dtype = np.float64
dtype = np.dtype(np.float64)
fill_value = np.nan
else:
dtype = np.dtype(np.object_)
Expand All @@ -644,9 +630,7 @@ def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
dtype = np.dtype(np.object_)

# in case we have a string that looked like a number
if is_extension_array_dtype(dtype):
pass
elif issubclass(np.dtype(dtype).type, (bytes, str)):
if issubclass(dtype.type, (bytes, str)):
dtype = np.dtype(np.object_)

fill_value = _ensure_dtype_type(fill_value, dtype)
Expand Down
46 changes: 1 addition & 45 deletions pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
import pytest

from pandas._libs.tslibs import NaT, tz_compare
from pandas._libs.tslibs import NaT

from pandas.core.dtypes.cast import maybe_promote
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -406,50 +406,6 @@ def test_maybe_promote_any_with_datetime64(
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)


def test_maybe_promote_datetimetz_with_any_numpy_dtype(
tz_aware_fixture, any_numpy_dtype_reduced
):
dtype = DatetimeTZDtype(tz=tz_aware_fixture)
fill_dtype = np.dtype(any_numpy_dtype_reduced)

# create array of given dtype; casts "1" to correct dtype
fill_value = np.array([1], dtype=fill_dtype)[0]

# filling datetimetz with any numpy dtype casts to object
expected_dtype = np.dtype(object)
exp_val_for_scalar = fill_value

_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)


def test_maybe_promote_datetimetz_with_datetimetz(tz_aware_fixture, tz_aware_fixture2):
dtype = DatetimeTZDtype(tz=tz_aware_fixture)
fill_dtype = DatetimeTZDtype(tz=tz_aware_fixture2)

# create array of given dtype; casts "1" to correct dtype
fill_value = pd.Series([10 ** 9], dtype=fill_dtype)[0]

# filling datetimetz with datetimetz casts to object, unless tz matches
exp_val_for_scalar = fill_value
if tz_compare(dtype.tz, fill_dtype.tz):
expected_dtype = dtype
else:
expected_dtype = np.dtype(object)

_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)


@pytest.mark.parametrize("fill_value", [None, np.nan, NaT])
def test_maybe_promote_datetimetz_with_na(tz_aware_fixture, fill_value):

dtype = DatetimeTZDtype(tz=tz_aware_fixture)

expected_dtype = dtype
exp_val_for_scalar = NaT

_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)


@pytest.mark.parametrize(
"fill_value",
[
Expand Down