Skip to content

Commit

Permalink
exploration: check if array-case is required
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vetinari committed Sep 22, 2019
1 parent 2a8691a commit d5aa77b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 89 deletions.
2 changes: 0 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,6 @@ def maybe_promote(dtype, fill_value=np.nan):
"""
if is_scalar(fill_value) or isinstance(fill_value, tuple):
return _maybe_promote_with_scalar(dtype, fill_value)
elif isinstance(fill_value, (np.ndarray, ABCSeries, ABCIndexClass)):
return _maybe_promote_with_array(dtype, fill_value)
else:
fill_type = type(fill_value).__name__
raise ValueError(
Expand Down
93 changes: 6 additions & 87 deletions pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def any_numpy_dtype_reduced(request):
return request.param


@pytest.fixture(
params=[(True, None), (True, object), (False, None)],
ids=["True-None", "True-object", "False-None"],
)
@pytest.fixture(params=[(False, None)], ids=["False-None"])
def box(request):
"""
Parametrized fixture determining whether/how to transform fill_value.
Expand Down Expand Up @@ -476,16 +473,7 @@ def test_maybe_promote_bytes_with_any(bytes_dtype, any_numpy_dtype_reduced, box)


# override parametrization of box to add special case for bytes
@pytest.mark.parametrize(
"box",
[
(True, None), # fill_value wrapped in array with default dtype
(True, "bytes"), # fill_value in array with generic bytes dtype
(True, object), # fill_value wrapped in array with object dtype
(False, None), # fill_value passed on as scalar
],
ids=["True-None", "True-bytes", "True-object", "False-None"],
)
@pytest.mark.parametrize("box", [(False, None)]) # fill_value passed on as scalar
def test_maybe_promote_any_with_bytes(any_numpy_dtype_reduced, bytes_dtype, box):
dtype = np.dtype(any_numpy_dtype_reduced)
fill_dtype = np.dtype(bytes_dtype)
Expand Down Expand Up @@ -547,16 +535,7 @@ def test_maybe_promote_datetime64_with_any(


# override parametrization of box to add special case for dt_dtype
@pytest.mark.parametrize(
"box",
[
(True, None), # fill_value wrapped in array with default dtype
(True, "dt_dtype"), # fill_value in array with explicit datetime dtype
(True, object), # fill_value wrapped in array with object dtype
(False, None), # fill_value passed on as scalar
],
ids=["True-None", "True-dt_dtype", "True-object", "False-None"],
)
@pytest.mark.parametrize("box", [(False, None)]) # fill_value passed on as scalar
@pytest.mark.parametrize(
"fill_value",
[
Expand Down Expand Up @@ -766,16 +745,7 @@ def test_maybe_promote_timedelta64_with_any(
ids=["pd.Timedelta", "np.timedelta64", "datetime.timedelta"],
)
# override parametrization of box to add special case for td_dtype
@pytest.mark.parametrize(
"box",
[
(True, None), # fill_value wrapped in array with default dtype
(True, "td_dtype"), # fill_value in array with explicit timedelta dtype
(True, object), # fill_value wrapped in array with object dtype
(False, None), # fill_value passed on as scalar
],
ids=["True-None", "True-td_dtype", "True-object", "False-None"],
)
@pytest.mark.parametrize("box", [(False, None)]) # fill_value passed on as scalar
def test_maybe_promote_any_with_timedelta64(
any_numpy_dtype_reduced, timedelta64_dtype, fill_value, box
):
Expand Down Expand Up @@ -832,17 +802,7 @@ def test_maybe_promote_string_with_any(string_dtype, any_numpy_dtype_reduced, bo


# override parametrization of box to add special case for str
@pytest.mark.parametrize(
"box",
[
# disabled due to too many xfails; see GH 23982 / 25425
(True, None), # fill_value wrapped in array with default dtype
(True, "str"), # fill_value wrapped in array with generic string-dtype
(True, object), # fill_value wrapped in array with object dtype
(False, None), # fill_value passed on as scalar
],
ids=["True-None", "True-str", "True-object", "False-None"],
)
@pytest.mark.parametrize("box", [(False, None)]) # fill_value passed on as scalar
def test_maybe_promote_any_with_string(any_numpy_dtype_reduced, string_dtype, box):
dtype = np.dtype(any_numpy_dtype_reduced)
fill_dtype = np.dtype(string_dtype)
Expand Down Expand Up @@ -922,14 +882,7 @@ def test_maybe_promote_any_with_object(any_numpy_dtype_reduced, object_dtype, bo
"fill_value", [None, np.nan, NaT, iNaT], ids=["None", "np.nan", "pd.NaT", "iNaT"]
)
# override parametrization of box, because default dtype for na is always float
@pytest.mark.parametrize(
"box",
[
(True, object), # fill_value wrapped in array with object dtype
(False, None), # fill_value passed on as scalar
],
ids=["True-object", "False-None"],
)
@pytest.mark.parametrize("box", [(False, None)]) # fill_value passed on as scalar
def test_maybe_promote_any_numpy_dtype_with_na(
any_numpy_dtype_reduced, fill_value, box
):
Expand Down Expand Up @@ -990,40 +943,6 @@ def test_maybe_promote_any_numpy_dtype_with_na(
)


@pytest.mark.parametrize("dim", [0, 2, 3])
def test_maybe_promote_dimensions(any_numpy_dtype_reduced, dim):
dtype = np.dtype(any_numpy_dtype_reduced)

# create 0-dim array of given dtype; casts "1" to correct dtype
fill_array = np.array(1, dtype=dtype)

# expand to desired dimension:
for _ in range(dim):
fill_array = np.expand_dims(fill_array, 0)

# test against 1-dimensional case
expected_dtype, expected_missing_value = maybe_promote(
dtype, np.array([1], dtype=dtype)
)

result_dtype, result_missing_value = maybe_promote(dtype, fill_array)

assert result_dtype == expected_dtype
# None == None, iNaT == iNaT, but np.nan != np.nan
assert (result_missing_value == expected_missing_value) or (
result_missing_value is np.nan and expected_missing_value is np.nan
)

# same again for _maybe_promote_with_array (for coverage)
result_dtype, result_missing_value = _maybe_promote_with_array(dtype, fill_array)

assert result_dtype == expected_dtype
# None == None, iNaT == iNaT, but np.nan != np.nan
assert (result_missing_value == expected_missing_value) or (
result_missing_value is np.nan and expected_missing_value is np.nan
)


def test_maybe_promote_raises(any_numpy_dtype):
msg = "fill_value must either be scalar, or a Series / Index / np.ndarra.*"
with pytest.raises(ValueError, match=msg):
Expand Down

0 comments on commit d5aa77b

Please sign in to comment.