8000 BUG: maybe_promote with dt64tz and mismatched NA by jbrockmendel · Pull Request #39743 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: maybe_promote with dt64tz and mismatched NA #39743

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 4 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
BUG: maybe_promote with dt64tz and mismatched NA
  • Loading branch information
jbrockmendel committed Feb 11, 2021
commit aa166a4e02403747d4fe2482f2a94765e26b44e8
66 changes: 33 additions & 33 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import numpy as np

from pandas._libs import lib, missing as libmissing, tslib
from pandas._libs import lib, tslib
from pandas._libs.tslibs import (
NaT,
OutOfBoundsDatetime,
Expand Down Expand Up @@ -87,7 +87,12 @@
ABCSeries,
)
from pandas.core.dtypes.inference import is_list_like
from pandas.core.dtypes.missing import is_valid_na_for_dtype, isna, notna
from pandas.core.dtypes.missing import (
is_valid_na_for_dtype,
isna,
na_value_for_dtype,
notna,
)

if TYPE_CHECKING:
from pandas import Series
Expand Down Expand Up @@ -530,16 +535,26 @@ def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
dtype = np.dtype(object)
return dtype, fill_value

kinds = ["i", "u", "f", "c", "m", "M"]
if is_valid_na_for_dtype(fill_value, dtype) and dtype.kind in kinds:
dtype = ensure_dtype_can_hold_na(dtype)
fv = na_value_for_dtype(dtype)
return dtype, fv

elif isna(fill_value):
dtype = np.dtype(object)
if fill_value is None:
# but we retain e.g. pd.NA
fill_value = np.nan
return dtype, fill_value

# returns tuple of (dtype, fill_value)
if issubclass(dtype.type, np.datetime64):
if isinstance(fill_value, datetime) and fill_value.tzinfo is not None:
# Trying to insert tzaware into tznaive, have to cast to object
dtype = np.dtype(np.object_)
elif is_integer(fill_value) or (is_float(fill_value) and not isna(fill_value)):
elif is_integer(fill_value) or is_float(fill_value):
dtype = np.dtype(np.object_)
elif is_valid_na_for_dtype(fill_value, dtype):
# e.g. pd.NA, which is not accepted by Timestamp constructor
fill_value = np.datetime64("NaT", "ns")
else:
try:
fill_value = Timestamp(fill_value).to_datetime64()
Expand All @@ -548,14 +563,11 @@ def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
elif issubclass(dtype.type, np.timedelta64):
if (
is_integer(fill_value)
or (is_float(fill_value) and not np.isnan(fill_value))
or is_float(fill_value)
or isinstance(fill_value, str)
):
# TODO: What about str that can be a timedelta?
dtype = np.dtype(np.object_)
elif is_valid_na_for_dtype(fill_value, dtype):
# e.g pd.NA, which is not accepted by the Timedelta constructor
fill_value = np.timedelta64("NaT", "ns")
else:
try:
fv = Timedelta(fill_value)
Expand All @@ -568,19 +580,14 @@ def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
else:
fill_value = fv.to_timedelta64()
elif isinstance(dtype, DatetimeTZDtype):
if isna(fill_value):
fill_value = NaT
elif not isinstance(fill_value, datetime):
if 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_):
dtype = np.dtype(np.object_)
Expand Down Expand Up @@ -629,30 +636,23 @@ def maybe_promote(dtype: DtypeObj, fill_value=np.nan):
# e.g. mst is np.complex128 and dtype is np.complex64
dtype = mst

elif fill_value is None or fill_value is libmissing.NA:
# Note: we already excluded dt64/td64 dtypes above
if is_float_dtype(dtype) or is_complex_dtype(dtype):
fill_value = np.nan
elif is_integer_dtype(dtype):
dtype = np.float64
fill_value = np.nan
else:
dtype = np.dtype(np.object_)
if fill_value is not libmissing.NA:
fill_value = np.nan
else:
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)):
dtype = np.dtype(np.object_)

dtype = sanitize_str_dtypes_to_object(dtype)
fill_value = _ensure_dtype_type(fill_value, dtype)
return dtype, fill_value


def sanitize_str_dtypes_to_object(dtype: DtypeObj) -> DtypeObj:
"""
Convert any numpy str/bytes dtypes to object.
"""
if isinstance(dtype, np.dtype) and dtype.kind in ["S", "U"]:
dtype = np.dtype(object)
return dtype


def _ensure_dtype_type(value, dtype: DtypeObj):
"""
Ensure that the given value is an instance of the given dtype.
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,17 @@ def test_maybe_promote_datetimetz_with_na(tz_aware_fixture, fill_value):
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)


def test_maybe_promote_datetimetz_with_mismatched_na(tz_aware_fixture):
fill_value = np.timedelta64("NaT")

dtype = DatetimeTZDtype(tz=tz_aware_fixture)

expected_dtype = np.dtype(object)
exp_val_for_scalar = fill_value

_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)


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