From 3ad8abd78625f677507ef78bc83e3cc903900c8b Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 26 Dec 2019 19:05:55 -0800 Subject: [PATCH 1/2] TST: fix maybe_promote dt64tz case, 323 xfails --- pandas/core/dtypes/cast.py | 9 +++++++++ pandas/tests/dtypes/cast/test_promote.py | 9 +-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 946070f8fad98..fa7b45ec4babd 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -6,6 +6,7 @@ from pandas._libs import lib, tslib, tslibs from pandas._libs.tslibs import NaT, OutOfBoundsDatetime, Period, iNaT +from pandas._libs.tslibs.timezones import tz_compare from pandas.util._validators import validate_bool_kwarg from .common import ( @@ -409,6 +410,14 @@ def maybe_promote(dtype, fill_value=np.nan): elif is_datetime64tz_dtype(dtype): 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 diff --git a/pandas/tests/dtypes/cast/test_promote.py b/pandas/tests/dtypes/cast/test_promote.py index 0939e35bd64fa..4bc20437f7512 100644 --- a/pandas/tests/dtypes/cast/test_promote.py +++ b/pandas/tests/dtypes/cast/test_promote.py @@ -8,7 +8,6 @@ import pytest from pandas._libs.tslibs import NaT -from pandas.compat import is_platform_windows from pandas.core.dtypes.cast import maybe_promote from pandas.core.dtypes.common import ( @@ -406,7 +405,6 @@ def test_maybe_promote_any_with_datetime64( _check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar) -@pytest.mark.xfail(reason="Fails to upcast to object") def test_maybe_promote_datetimetz_with_any_numpy_dtype( tz_aware_fixture, any_numpy_dtype_reduced ): @@ -419,6 +417,7 @@ def test_maybe_promote_datetimetz_with_any_numpy_dtype( # filling datetimetz with any numpy dtype casts to object expected_dtype = np.dtype(object) exp_val_for_scalar = fill_value + print(421, dtype.type, type(fill_value)) _check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar) @@ -427,11 +426,6 @@ def test_maybe_promote_datetimetz_with_datetimetz(tz_aware_fixture, tz_aware_fix dtype = DatetimeTZDtype(tz=tz_aware_fixture) fill_dtype = DatetimeTZDtype(tz=tz_aware_fixture2) - from dateutil.tz import tzlocal - - if is_platform_windows() and tz_aware_fixture2 == tzlocal(): - pytest.xfail("Cannot process fill_value with this dtype, see GH 24310") - # create array of given dtype; casts "1" to correct dtype fill_value = pd.Series([10 ** 9], dtype=fill_dtype)[0] @@ -441,7 +435,6 @@ def test_maybe_promote_datetimetz_with_datetimetz(tz_aware_fixture, tz_aware_fix expected_dtype = dtype else: expected_dtype = np.dtype(object) - pytest.xfail("fails to cast to object") _check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar) From a06d2c15f1ca9b625e2f78f914d552dc36279145 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 26 Dec 2019 19:07:12 -0800 Subject: [PATCH 2/2] remove debug print --- pandas/tests/dtypes/cast/test_promote.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/dtypes/cast/test_promote.py b/pandas/tests/dtypes/cast/test_promote.py index 4bc20437f7512..69f8f46356a4d 100644 --- a/pandas/tests/dtypes/cast/test_promote.py +++ b/pandas/tests/dtypes/cast/test_promote.py @@ -417,7 +417,6 @@ def test_maybe_promote_datetimetz_with_any_numpy_dtype( # filling datetimetz with any numpy dtype casts to object expected_dtype = np.dtype(object) exp_val_for_scalar = fill_value - print(421, dtype.type, type(fill_value)) _check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)