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
Prev Previous commit
Next Next commit
Merge branch 'master' into ref-maybe_promote-na
  • Loading branch information
jbrockmendel committed Feb 12, 2021
commit 4b20d4c1a7945751987facc82613ddad76a2146d
22 changes: 4 additions & 18 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,6 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
fill_value = np.timedelta64("NaT", "ns")
else:
fill_value = fv.to_timedelta64()
elif isinstance(dtype, DatetimeTZDtype):
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_float(fill_value):
if issubclass(dtype.type, np.bool_):
Expand Down Expand Up @@ -638,20 +630,14 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
else:
dtype = np.dtype(np.object_)

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

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
81 changes: 0 additions & 81 deletions pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
is_scalar,
is_timedelta64_dtype,
)
from pandas.core.dtypes.dtypes import DatetimeTZDtype
from pandas.core.dtypes.missing import isna

import pandas as pd
Expand Down Expand Up @@ -406,86 +405,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)


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",
[
pd.Timestamp("now"),
np.datetime64("now"),
datetime.datetime.now(),
datetime.date.today(),
],
ids=["pd.Timestamp", "np.datetime64", "datetime.datetime", "datetime.date"],
)
def test_maybe_promote_any_numpy_dtype_with_datetimetz(
any_numpy_dtype_reduced, tz_aware_fixture, fill_value
):
dtype = np.dtype(any_numpy_dtype_reduced)
fill_dtype = DatetimeTZDtype(tz=tz_aware_fixture)

fill_value = pd.Series([fill_value], dtype=fill_dtype)[0]

# filling any numpy dtype with datetimetz 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_timedelta64_with_any(timedelta64_dtype, any_numpy_dtype_reduced):
dtype = np.dtype(timedelta64_dtype)
fill_dtype = np.dtype(any_numpy_dtype_reduced)
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0