Skip to content

REF: unwrap PandasArray earlier #43728

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

Merged
merged 3 commits into from
Sep 25, 2021
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
9 changes: 8 additions & 1 deletion pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@
is_object_dtype,
is_timedelta64_ns_dtype,
)
from pandas.core.dtypes.dtypes import DatetimeTZDtype
from pandas.core.dtypes.dtypes import (
DatetimeTZDtype,
PandasDtype,
)
from pandas.core.dtypes.generic import (
ABCExtensionArray,
ABCIndex,
Expand Down Expand Up @@ -494,6 +497,10 @@ def sanitize_array(
if isinstance(data, ma.MaskedArray):
data = sanitize_masked_array(data)

if isinstance(dtype, PandasDtype):
# Avoid ending up with a PandasArray
dtype = dtype.numpy_dtype

# extract ndarray or ExtensionArray, ensure we have no PandasArray
data = extract_array(data, extract_numpy=True)

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
DatetimeTZDtype,
ExtensionDtype,
IntervalDtype,
PandasDtype,
PeriodDtype,
)
from pandas.core.dtypes.generic import (
Expand Down Expand Up @@ -1305,6 +1306,9 @@ def astype_array_safe(
raise TypeError(msg)

dtype = pandas_dtype(dtype)
if isinstance(dtype, PandasDtype):
# Ensure we don't end up with a PandasArray
dtype = dtype.numpy_dtype

try:
new_values = astype_array(values, dtype, copy=copy)
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,6 @@ def interpolate(
**kwargs,
)

interp_values = maybe_coerce_values(interp_values)
nbs = [self.make_block_same_class(interp_values)]
return self._maybe_downcast(nbs, downcast)

Expand Down Expand Up @@ -1903,10 +1902,7 @@ def maybe_coerce_values(values: ArrayLike) -> ArrayLike:
-------
values : np.ndarray or ExtensionArray
"""

# Note: the only test that needs extract_array here is one where we
# pass PandasDtype to Series.astype, then need to extract PandasArray here.
values = extract_array(values, extract_numpy=True)
# Caller is responsible for ensuring PandasArray is already extracted.

if isinstance(values, np.ndarray):
values = ensure_wrapped_if_datetimelike(values)
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
ensure_block_shape,
extend_blocks,
get_block_type,
maybe_coerce_values,
new_block,
)
from pandas.core.internals.ops import (
Expand Down Expand Up @@ -989,7 +988,6 @@ def iget(self, i: int) -> SingleBlockManager:

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

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/extension/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,19 @@ def test_merge(self, data, na_value):
# Fails creating expected (key column becomes a PandasDtype because)
super().test_merge(data, na_value)

@pytest.mark.parametrize(
"in_frame",
[
True,
pytest.param(
False,
marks=pytest.mark.xfail(reason="PandasArray inconsistently extracted"),
),
],
)
def test_concat(self, data, in_frame):
super().test_concat(data, in_frame)


class TestSetitem(BaseNumPyTests, base.BaseSetitemTests):
@skip_nested
Expand Down