Skip to content
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

BUG: require arraylike in infer_dtype_from_array #38473

Merged
merged 15 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
If False, scalar/array belongs to pandas extension types is inferred as
object
"""
if is_scalar(val):
if not is_list_like(val):
return infer_dtype_from_scalar(val, pandas_dtype=pandas_dtype)
return infer_dtype_from_array(val, pandas_dtype=pandas_dtype)

Expand Down Expand Up @@ -815,7 +815,7 @@ def infer_dtype_from_array(
return arr.dtype, arr

if not is_list_like(arr):
arr = [arr]
raise TypeError("'arr' must be list-like")

if pandas_dtype and is_extension_array_dtype(arr):
return arr.dtype, arr
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pandas._typing import ArrayLike, Axis, DtypeObj
from pandas.compat._optional import import_optional_dependency

from pandas.core.dtypes.cast import infer_dtype_from_array
from pandas.core.dtypes.cast import infer_dtype_from
from pandas.core.dtypes.common import (
ensure_float64,
is_integer_dtype,
Expand Down Expand Up @@ -40,7 +40,7 @@ def mask_missing(arr: ArrayLike, values_to_mask) -> np.ndarray:
# When called from Block.replace/replace_list, values_to_mask is a scalar
# known to be holdable by arr.
# When called from Series._single_replace, values_to_mask is tuple or list
dtype, values_to_mask = infer_dtype_from_array(values_to_mask)
dtype, values_to_mask = infer_dtype_from(values_to_mask)
values_to_mask = np.array(values_to_mask, dtype=dtype)

na_mask = isna(values_to_mask)
Expand Down
23 changes: 19 additions & 4 deletions pandas/tests/dtypes/cast/test_infer_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,29 @@ def test_infer_dtype_from_scalar_errors():


@pytest.mark.parametrize(
"arr, expected, pandas_dtype",
"value, expected, pandas_dtype",
[
("foo", np.object_, False),
(b"foo", np.object_, False),
(1, np.int_, False),
(1, np.int64, False),
(1.5, np.float_, False),
(np.datetime64("2016-01-01"), np.dtype("M8[ns]"), False),
(Timestamp("20160101"), np.dtype("M8[ns]"), False),
(Timestamp("20160101", tz="UTC"), np.object_, False),
(Timestamp("20160101", tz="UTC"), "datetime64[ns, UTC]", True),
jreback marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_infer_dtype_from_scalar(value, expected, pandas_dtype):
dtype, _ = infer_dtype_from_scalar(value, pandas_dtype=pandas_dtype)
assert is_dtype_equal(dtype, expected)

with pytest.raises(TypeError, match="must be list-like"):
infer_dtype_from_array(value, pandas_dtype=pandas_dtype)


@pytest.mark.parametrize(
"arr, expected, pandas_dtype",
[
([1], np.int_, False),
(np.array([1], dtype=np.int64), np.int64, False),
([np.nan, 1, ""], np.object_, False),
Expand All @@ -155,8 +172,6 @@ def test_infer_dtype_from_scalar_errors():
(Categorical([1, 2, 3]), np.int64, False),
(Categorical(list("aabc")), "category", True),
(Categorical([1, 2, 3]), "category", True),
(Timestamp("20160101"), np.object_, False),
(np.datetime64("2016-01-01"), np.dtype("=M8[D]"), False),
(date_range("20160101", periods=3), np.dtype("=M8[ns]"), False),
(
date_range("20160101", periods=3, tz="US/Eastern"),
Expand Down