Skip to content

Commit 22dd265

Browse files
committed
BUG: require arraylike in infer_dtype_from_array
1 parent bd39ded commit 22dd265

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, Any]:
712712
If False, scalar/array belongs to pandas extension types is inferred as
713713
object
714714
"""
715-
if is_scalar(val):
715+
if not is_list_like(val):
716716
return infer_dtype_from_scalar(val, pandas_dtype=pandas_dtype)
717717
return infer_dtype_from_array(val, pandas_dtype=pandas_dtype)
718718

@@ -853,7 +853,7 @@ def infer_dtype_from_array(
853853
return arr.dtype, arr
854854

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

858858
if pandas_dtype and is_extension_array_dtype(arr):
859859
return arr.dtype, arr

pandas/core/missing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pandas._typing import ArrayLike, Axis, DtypeObj
1111
from pandas.compat._optional import import_optional_dependency
1212

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

4646
na_mask = isna(values_to_mask)

pandas/tests/dtypes/cast/test_infer_dtype.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,29 @@ def test_infer_dtype_from_scalar_errors():
137137

138138

139139
@pytest.mark.parametrize(
140-
"arr, expected, pandas_dtype",
140+
"value, expected, pandas_dtype",
141141
[
142142
("foo", np.object_, False),
143143
(b"foo", np.object_, False),
144144
(1, np.int_, False),
145145
(1.5, np.float_, False),
146+
(np.datetime64("2016-01-01"), np.dtype("M8[ns]"), False),
147+
(Timestamp("20160101"), np.dtype("M8[ns]"), False),
148+
(Timestamp("20160101", tz="UTC"), np.object_, False),
149+
(Timestamp("20160101", tz="UTC"), "datetime64[ns, UTC]", True),
150+
],
151+
)
152+
def test_infer_dtype_from_scalar(value, expected, pandas_dtype):
153+
dtype, _ = infer_dtype_from_scalar(value, pandas_dtype=pandas_dtype)
154+
assert is_dtype_equal(dtype, expected)
155+
156+
with pytest.raises(TypeError, match="must be list-like"):
157+
infer_dtype_from_array(value, pandas_dtype=pandas_dtype)
158+
159+
160+
@pytest.mark.parametrize(
161+
"arr, expected, pandas_dtype",
162+
[
146163
([1], np.int_, False),
147164
(np.array([1], dtype=np.int64), np.int64, False),
148165
([np.nan, 1, ""], np.object_, False),
@@ -151,8 +168,6 @@ def test_infer_dtype_from_scalar_errors():
151168
(Categorical([1, 2, 3]), np.int64, False),
152169
(Categorical(list("aabc")), "category", True),
153170
(Categorical([1, 2, 3]), "category", True),
154-
(Timestamp("20160101"), np.object_, False),
155-
(np.datetime64("2016-01-01"), np.dtype("=M8[D]"), False),
156171
(date_range("20160101", periods=3), np.dtype("=M8[ns]"), False),
157172
(
158173
date_range("20160101", periods=3, tz="US/Eastern"),

0 commit comments

Comments
 (0)