Skip to content

Commit c4faad9

Browse files
jbrockmendelproost
authored andcommitted
TST: port tests from pandas-dev#23982 (pandas-dev#28832)
1 parent c8eb001 commit c4faad9

File tree

1 file changed

+86
-6
lines changed

1 file changed

+86
-6
lines changed

pandas/tests/dtypes/cast/test_promote.py

+86-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
is_integer_dtype,
2020
is_object_dtype,
2121
is_scalar,
22+
is_string_dtype,
2223
is_timedelta64_dtype,
2324
)
2425
from pandas.core.dtypes.dtypes import DatetimeTZDtype
@@ -500,14 +501,93 @@ def test_maybe_promote_any_with_bool(any_numpy_dtype_reduced, box):
500501
)
501502

502503

503-
def test_maybe_promote_bytes_with_any():
504-
# placeholder due to too many xfails; see GH 23982 / 25425
505-
pass
504+
def test_maybe_promote_bytes_with_any(bytes_dtype, any_numpy_dtype_reduced, box):
505+
dtype = np.dtype(bytes_dtype)
506+
fill_dtype = np.dtype(any_numpy_dtype_reduced)
507+
boxed, box_dtype = box # read from parametrized fixture
508+
509+
if issubclass(fill_dtype.type, np.bytes_):
510+
if not boxed or box_dtype == object:
511+
pytest.xfail("falsely upcasts to object")
512+
# takes the opinion that bool dtype has no missing value marker
513+
else:
514+
pytest.xfail("wrong missing value marker")
515+
else:
516+
if boxed and box_dtype is None:
517+
pytest.xfail("does not upcast to object")
518+
if (
519+
is_integer_dtype(fill_dtype)
520+
or is_float_dtype(fill_dtype)
521+
or is_complex_dtype(fill_dtype)
522+
or is_object_dtype(fill_dtype)
523+
or is_timedelta64_dtype(fill_dtype)
524+
) and not boxed:
525+
pytest.xfail("does not upcast to object")
526+
527+
# create array of given dtype; casts "1" to correct dtype
528+
fill_value = np.array([1], dtype=fill_dtype)[0]
529+
530+
# filling bytes with anything but bytes casts to object
531+
expected_dtype = (
532+
dtype if issubclass(fill_dtype.type, np.bytes_) else np.dtype(object)
533+
)
534+
exp_val_for_scalar = fill_value
535+
exp_val_for_array = None if issubclass(fill_dtype.type, np.bytes_) else np.nan
536+
537+
_check_promote(
538+
dtype,
539+
fill_value,
540+
boxed,
541+
box_dtype,
542+
expected_dtype,
543+
exp_val_for_scalar,
544+
exp_val_for_array,
545+
)
506546

507547

508-
def test_maybe_promote_any_with_bytes():
509-
# placeholder due to too many xfails; see GH 23982 / 25425
510-
pass
548+
def test_maybe_promote_any_with_bytes(any_numpy_dtype_reduced, bytes_dtype, box):
549+
dtype = np.dtype(any_numpy_dtype_reduced)
550+
fill_dtype = np.dtype(bytes_dtype)
551+
boxed, box_dtype = box # read from parametrized fixture
552+
553+
if issubclass(dtype.type, np.bytes_):
554+
if not boxed or box_dtype == object:
555+
pytest.xfail("falsely upcasts to object")
556+
# takes the opinion that bool dtype has no missing value marker
557+
else:
558+
pytest.xfail("wrong missing value marker")
559+
else:
560+
pass
561+
if (
562+
boxed
563+
and (box_dtype == "bytes" or box_dtype is None)
564+
and not (is_string_dtype(dtype) or dtype == bool)
565+
):
566+
pytest.xfail("does not upcast to object")
567+
if not boxed and is_datetime_or_timedelta_dtype(dtype):
568+
pytest.xfail("raises error")
569+
570+
# create array of given dtype
571+
fill_value = b"abc"
572+
573+
# special case for box_dtype (cannot use fixture in parametrization)
574+
box_dtype = fill_dtype if box_dtype == "bytes" else box_dtype
575+
576+
# filling bytes with anything but bytes casts to object
577+
expected_dtype = dtype if issubclass(dtype.type, np.bytes_) else np.dtype(object)
578+
# output is not a generic bytes, but corresponds to expected_dtype
579+
exp_val_for_scalar = np.array([fill_value], dtype=expected_dtype)[0]
580+
exp_val_for_array = None if issubclass(dtype.type, np.bytes_) else np.nan
581+
582+
_check_promote(
583+
dtype,
584+
fill_value,
585+
boxed,
586+
box_dtype,
587+
expected_dtype,
588+
exp_val_for_scalar,
589+
exp_val_for_array,
590+
)
511591

512592

513593
def test_maybe_promote_datetime64_with_any(

0 commit comments

Comments
 (0)