Skip to content

Commit c948a5a

Browse files
jbrockmendelluckyvs1
authored andcommitted
REF: implement construct_2d_arraylike_from_scalar (pandas-dev#38583)
1 parent 980de86 commit c948a5a

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

pandas/core/dtypes/cast.py

+22
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,28 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:
15261526
return np.find_common_type(types, [])
15271527

15281528

1529+
def construct_2d_arraylike_from_scalar(
1530+
value: Scalar, length: int, width: int, dtype: np.dtype, copy: bool
1531+
) -> np.ndarray:
1532+
1533+
if dtype.kind in ["m", "M"]:
1534+
value = maybe_unbox_datetimelike(value, dtype)
1535+
1536+
# Attempt to coerce to a numpy array
1537+
try:
1538+
arr = np.array(value, dtype=dtype, copy=copy)
1539+
except (ValueError, TypeError) as err:
1540+
raise TypeError(
1541+
f"DataFrame constructor called with incompatible data and dtype: {err}"
1542+
) from err
1543+
1544+
if arr.ndim != 0:
1545+
raise ValueError("DataFrame constructor not properly called!")
1546+
1547+
shape = (length, width)
1548+
return np.full(shape, arr)
1549+
1550+
15291551
def construct_1d_arraylike_from_scalar(
15301552
value: Scalar, length: int, dtype: Optional[DtypeObj]
15311553
) -> ArrayLike:

pandas/core/frame.py

+5-19
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@
8585

8686
from pandas.core.dtypes.cast import (
8787
construct_1d_arraylike_from_scalar,
88+
construct_2d_arraylike_from_scalar,
8889
find_common_type,
8990
infer_dtype_from_scalar,
9091
invalidate_string_dtypes,
9192
maybe_box_datetimelike,
9293
maybe_convert_platform,
9394
maybe_downcast_to_dtype,
9495
maybe_infer_to_datetimelike,
95-
maybe_unbox_datetimelike,
9696
validate_numeric_casting,
9797
)
9898
from pandas.core.dtypes.common import (
@@ -595,31 +595,17 @@ def __init__(
595595

596596
# For data is a scalar extension dtype
597597
if is_extension_array_dtype(dtype):
598+
# TODO(EA2D): special case not needed with 2D EAs
598599

599600
values = [
600601
construct_1d_arraylike_from_scalar(data, len(index), dtype)
601602
for _ in range(len(columns))
602603
]
603604
mgr = arrays_to_mgr(values, columns, index, columns, dtype=None)
604605
else:
605-
if dtype.kind in ["m", "M"]:
606-
data = maybe_unbox_datetimelike(data, dtype)
607-
608-
# Attempt to coerce to a numpy array
609-
try:
610-
arr = np.array(data, dtype=dtype, copy=copy)
611-
except (ValueError, TypeError) as err:
612-
exc = TypeError(
613-
"DataFrame constructor called with "
614-
f"incompatible data and dtype: {err}"
615-
)
616-
raise exc from err
617-
618-
if arr.ndim != 0:
619-
raise ValueError("DataFrame constructor not properly called!")
620-
621-
shape = (len(index), len(columns))
622-
values = np.full(shape, arr)
606+
values = construct_2d_arraylike_from_scalar(
607+
data, len(index), len(columns), dtype, copy
608+
)
623609

624610
mgr = init_ndarray(
625611
values, index, columns, dtype=values.dtype, copy=False

0 commit comments

Comments
 (0)