Skip to content

Commit 85d9e54

Browse files
jbrockmendeljreback
authored andcommitted
TST: fix maybe_promote dt64tz case, 323 xfails (#30506)
1 parent 012b0bd commit 85d9e54

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

pandas/core/dtypes/cast.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pandas._libs import lib, tslib, tslibs
88
from pandas._libs.tslibs import NaT, OutOfBoundsDatetime, Period, iNaT
9+
from pandas._libs.tslibs.timezones import tz_compare
910
from pandas.util._validators import validate_bool_kwarg
1011

1112
from .common import (
@@ -409,6 +410,14 @@ def maybe_promote(dtype, fill_value=np.nan):
409410
elif is_datetime64tz_dtype(dtype):
410411
if isna(fill_value):
411412
fill_value = NaT
413+
elif not isinstance(fill_value, datetime):
414+
dtype = np.dtype(np.object_)
415+
elif fill_value.tzinfo is None:
416+
dtype = np.dtype(np.object_)
417+
elif not tz_compare(fill_value.tzinfo, dtype.tz):
418+
# TODO: sure we want to cast here?
419+
dtype = np.dtype(np.object_)
420+
412421
elif is_extension_array_dtype(dtype) and isna(fill_value):
413422
fill_value = dtype.na_value
414423

pandas/tests/dtypes/cast/test_promote.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import pytest
99

1010
from pandas._libs.tslibs import NaT
11-
from pandas.compat import is_platform_windows
1211

1312
from pandas.core.dtypes.cast import maybe_promote
1413
from pandas.core.dtypes.common import (
@@ -406,7 +405,6 @@ def test_maybe_promote_any_with_datetime64(
406405
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)
407406

408407

409-
@pytest.mark.xfail(reason="Fails to upcast to object")
410408
def test_maybe_promote_datetimetz_with_any_numpy_dtype(
411409
tz_aware_fixture, any_numpy_dtype_reduced
412410
):
@@ -427,11 +425,6 @@ def test_maybe_promote_datetimetz_with_datetimetz(tz_aware_fixture, tz_aware_fix
427425
dtype = DatetimeTZDtype(tz=tz_aware_fixture)
428426
fill_dtype = DatetimeTZDtype(tz=tz_aware_fixture2)
429427

430-
from dateutil.tz import tzlocal
431-
432-
if is_platform_windows() and tz_aware_fixture2 == tzlocal():
433-
pytest.xfail("Cannot process fill_value with this dtype, see GH 24310")
434-
435428
# create array of given dtype; casts "1" to correct dtype
436429
fill_value = pd.Series([10 ** 9], dtype=fill_dtype)[0]
437430

@@ -441,7 +434,6 @@ def test_maybe_promote_datetimetz_with_datetimetz(tz_aware_fixture, tz_aware_fix
441434
expected_dtype = dtype
442435
else:
443436
expected_dtype = np.dtype(object)
444-
pytest.xfail("fails to cast to object")
445437

446438
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)
447439

0 commit comments

Comments
 (0)