Skip to content

Commit 764045d

Browse files
authored
REF: unwrap PandasArray earlier (#43728)
1 parent 5441d4e commit 764045d

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

pandas/core/construction.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
is_object_dtype,
5050
is_timedelta64_ns_dtype,
5151
)
52-
from pandas.core.dtypes.dtypes import DatetimeTZDtype
52+
from pandas.core.dtypes.dtypes import (
53+
DatetimeTZDtype,
54+
PandasDtype,
55+
)
5356
from pandas.core.dtypes.generic import (
5457
ABCExtensionArray,
5558
ABCIndex,
@@ -494,6 +497,10 @@ def sanitize_array(
494497
if isinstance(data, ma.MaskedArray):
495498
data = sanitize_masked_array(data)
496499

500+
if isinstance(dtype, PandasDtype):
501+
# Avoid ending up with a PandasArray
502+
dtype = dtype.numpy_dtype
503+
497504
# extract ndarray or ExtensionArray, ensure we have no PandasArray
498505
data = extract_array(data, extract_numpy=True)
499506

pandas/core/dtypes/cast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
DatetimeTZDtype,
7878
ExtensionDtype,
7979
IntervalDtype,
80+
PandasDtype,
8081
PeriodDtype,
8182
)
8283
from pandas.core.dtypes.generic import (
@@ -1305,6 +1306,9 @@ def astype_array_safe(
13051306
raise TypeError(msg)
13061307

13071308
dtype = pandas_dtype(dtype)
1309+
if isinstance(dtype, PandasDtype):
1310+
# Ensure we don't end up with a PandasArray
1311+
dtype = dtype.numpy_dtype
13081312

13091313
try:
13101314
new_values = astype_array(values, dtype, copy=copy)

pandas/core/internals/blocks.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,6 @@ def interpolate(
11281128
**kwargs,
11291129
)
11301130

1131-
interp_values = maybe_coerce_values(interp_values)
11321131
nbs = [self.make_block_same_class(interp_values)]
11331132
return self._maybe_downcast(nbs, downcast)
11341133

@@ -1903,10 +1902,7 @@ def maybe_coerce_values(values: ArrayLike) -> ArrayLike:
19031902
-------
19041903
values : np.ndarray or ExtensionArray
19051904
"""
1906-
1907-
# Note: the only test that needs extract_array here is one where we
1908-
# pass PandasDtype to Series.astype, then need to extract PandasArray here.
1909-
values = extract_array(values, extract_numpy=True)
1905+
# Caller is responsible for ensuring PandasArray is already extracted.
19101906

19111907
if isinstance(values, np.ndarray):
19121908
values = ensure_wrapped_if_datetimelike(values)

pandas/core/internals/managers.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
ensure_block_shape,
7272
extend_blocks,
7373
get_block_type,
74-
maybe_coerce_values,
7574
new_block,
7675
)
7776
from pandas.core.internals.ops import (
@@ -989,7 +988,6 @@ def iget(self, i: int) -> SingleBlockManager:
989988

990989
# shortcut for select a single-dim from a 2-dim BM
991990
bp = BlockPlacement(slice(0, len(values)))
992-
values = maybe_coerce_values(values)
993991
nb = type(block)(values, placement=bp, ndim=1)
994992
return SingleBlockManager(nb, self.axes[1])
995993

pandas/tests/extension/test_numpy.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,19 @@ def test_merge(self, data, na_value):
336336
# Fails creating expected (key column becomes a PandasDtype because)
337337
super().test_merge(data, na_value)
338338

339+
@pytest.mark.parametrize(
340+
"in_frame",
341+
[
342+
True,
343+
pytest.param(
344+
False,
345+
marks=pytest.mark.xfail(reason="PandasArray inconsistently extracted"),
346+
),
347+
],
348+
)
349+
def test_concat(self, data, in_frame):
350+
super().test_concat(data, in_frame)
351+
339352

340353
class TestSetitem(BaseNumPyTests, base.BaseSetitemTests):
341354
@skip_nested

0 commit comments

Comments
 (0)