Skip to content

Commit 803bdd2

Browse files
authored
REF: simplify casting of scalars to array (#38313)
1 parent b23bba9 commit 803bdd2

File tree

3 files changed

+8
-73
lines changed

3 files changed

+8
-73
lines changed

pandas/core/dtypes/cast.py

+1-30
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
ints_to_pytimedelta,
3434
)
3535
from pandas._libs.tslibs.timezones import tz_compare
36-
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar, Shape
36+
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar
3737
from pandas.util._validators import validate_bool_kwarg
3838

3939
from pandas.core.dtypes.common import (
@@ -1563,35 +1563,6 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
15631563
return np.find_common_type(types, [])
15641564

15651565

1566-
def cast_scalar_to_array(
1567-
shape: Shape, value: Scalar, dtype: Optional[DtypeObj] = None
1568-
) -> np.ndarray:
1569-
"""
1570-
Create np.ndarray of specified shape and dtype, filled with values.
1571-
1572-
Parameters
1573-
----------
1574-
shape : tuple
1575-
value : scalar value
1576-
dtype : np.dtype, optional
1577-
dtype to coerce
1578-
1579-
Returns
1580-
-------
1581-
ndarray of shape, filled with value, of specified / inferred dtype
1582-
1583-
"""
1584-
if dtype is None:
1585-
dtype, fill_value = infer_dtype_from_scalar(value)
1586-
else:
1587-
fill_value = value
1588-
1589-
values = np.empty(shape, dtype=dtype)
1590-
values.fill(fill_value)
1591-
1592-
return values
1593-
1594-
15951566
def construct_1d_arraylike_from_scalar(
15961567
value: Scalar, length: int, dtype: DtypeObj
15971568
) -> ArrayLike:

pandas/core/frame.py

+6-17
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
)
7979

8080
from pandas.core.dtypes.cast import (
81-
cast_scalar_to_array,
8281
construct_1d_arraylike_from_scalar,
8382
find_common_type,
8483
infer_dtype_from_scalar,
@@ -615,9 +614,8 @@ def __init__(
615614
if arr.ndim != 0:
616615
raise ValueError("DataFrame constructor not properly called!")
617616

618-
values = cast_scalar_to_array(
619-
(len(index), len(columns)), data, dtype=dtype
620-
)
617+
shape = (len(index), len(columns))
618+
values = np.full(shape, arr)
621619

622620
mgr = init_ndarray(
623621
values, index, columns, dtype=values.dtype, copy=False
@@ -3914,20 +3912,11 @@ def reindexer(value):
39143912

39153913
else:
39163914
# cast ignores pandas dtypes. so save the dtype first
3917-
infer_dtype, _ = infer_dtype_from_scalar(value, pandas_dtype=True)
3915+
infer_dtype, fill_value = infer_dtype_from_scalar(value, pandas_dtype=True)
39183916

3919-
# upcast
3920-
if is_extension_array_dtype(infer_dtype):
3921-
value = construct_1d_arraylike_from_scalar(
3922-
value, len(self.index), infer_dtype
3923-
)
3924-
else:
3925-
# pandas\core\frame.py:3827: error: Argument 1 to
3926-
# "cast_scalar_to_array" has incompatible type "int"; expected
3927-
# "Tuple[Any, ...]" [arg-type]
3928-
value = cast_scalar_to_array(
3929-
len(self.index), value # type: ignore[arg-type]
3930-
)
3917+
value = construct_1d_arraylike_from_scalar(
3918+
fill_value, len(self), infer_dtype
3919+
)
39313920

39323921
value = maybe_cast_to_datetime(value, infer_dtype)
39333922

pandas/tests/dtypes/cast/test_infer_dtype.py

+1-26
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
from pandas.core.dtypes.cast import (
7-
cast_scalar_to_array,
8-
infer_dtype_from_array,
9-
infer_dtype_from_scalar,
10-
)
6+
from pandas.core.dtypes.cast import infer_dtype_from_array, infer_dtype_from_scalar
117
from pandas.core.dtypes.common import is_dtype_equal
128

139
from pandas import (
@@ -19,7 +15,6 @@
1915
Timestamp,
2016
date_range,
2117
)
22-
import pandas._testing as tm
2318

2419

2520
@pytest.fixture(params=[True, False])
@@ -176,23 +171,3 @@ def test_infer_dtype_from_scalar_errors():
176171
def test_infer_dtype_from_array(arr, expected, pandas_dtype):
177172
dtype, _ = infer_dtype_from_array(arr, pandas_dtype=pandas_dtype)
178173
assert is_dtype_equal(dtype, expected)
179-
180-
181-
@pytest.mark.parametrize(
182-
"obj,dtype",
183-
[
184-
(1, np.int64),
185-
(1.1, np.float64),
186-
(Timestamp("2011-01-01"), "datetime64[ns]"),
187-
(Timestamp("2011-01-01", tz="US/Eastern"), object),
188-
(Period("2011-01-01", freq="D"), object),
189-
],
190-
)
191-
def test_cast_scalar_to_array(obj, dtype):
192-
shape = (3, 2)
193-
194-
exp = np.empty(shape, dtype=dtype)
195-
exp.fill(obj)
196-
197-
arr = cast_scalar_to_array(shape, obj, dtype=dtype)
198-
tm.assert_numpy_array_equal(arr, exp)

0 commit comments

Comments
 (0)