|
27 | 27 | CategoricalDtype, ExtensionDtype, PandasExtensionDtype)
|
28 | 28 | from pandas.core.dtypes.generic import (
|
29 | 29 | ABCDataFrame, ABCDatetimeIndex, ABCExtensionArray, ABCIndexClass,
|
30 |
| - ABCSeries) |
| 30 | + ABCPandasArray, ABCSeries) |
31 | 31 | from pandas.core.dtypes.missing import (
|
32 | 32 | _isna_compat, array_equivalent, isna, notna)
|
33 | 33 |
|
34 | 34 | import pandas.core.algorithms as algos
|
35 | 35 | from pandas.core.arrays import (
|
36 |
| - Categorical, DatetimeArray, ExtensionArray, TimedeltaArray) |
| 36 | + Categorical, DatetimeArray, ExtensionArray, PandasDtype, TimedeltaArray) |
37 | 37 | from pandas.core.base import PandasObject
|
38 | 38 | import pandas.core.common as com
|
39 | 39 | from pandas.core.indexes.datetimes import DatetimeIndex
|
@@ -576,23 +576,14 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
|
576 | 576 |
|
577 | 577 | return self.make_block(Categorical(self.values, dtype=dtype))
|
578 | 578 |
|
579 |
| - # convert dtypes if needed |
580 | 579 | dtype = pandas_dtype(dtype)
|
| 580 | + |
581 | 581 | # astype processing
|
582 | 582 | if is_dtype_equal(self.dtype, dtype):
|
583 | 583 | if copy:
|
584 | 584 | return self.copy()
|
585 | 585 | return self
|
586 | 586 |
|
587 |
| - klass = None |
588 |
| - if is_sparse(self.values): |
589 |
| - # special case sparse, Series[Sparse].astype(object) is sparse |
590 |
| - klass = ExtensionBlock |
591 |
| - elif is_object_dtype(dtype): |
592 |
| - klass = ObjectBlock |
593 |
| - elif is_extension_array_dtype(dtype): |
594 |
| - klass = ExtensionBlock |
595 |
| - |
596 | 587 | try:
|
597 | 588 | # force the copy here
|
598 | 589 | if values is None:
|
@@ -624,7 +615,7 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
|
624 | 615 | pass
|
625 | 616 |
|
626 | 617 | newb = make_block(values, placement=self.mgr_locs,
|
627 |
| - klass=klass, ndim=self.ndim) |
| 618 | + ndim=self.ndim) |
628 | 619 | except Exception: # noqa: E722
|
629 | 620 | if errors == 'raise':
|
630 | 621 | raise
|
@@ -3041,6 +3032,13 @@ def get_block_type(values, dtype=None):
|
3041 | 3032 |
|
3042 | 3033 | def make_block(values, placement, klass=None, ndim=None, dtype=None,
|
3043 | 3034 | fastpath=None):
|
| 3035 | + # Ensure that we don't allow PandasArray / PandasDtype in internals. |
| 3036 | + # For now, blocks should be backed by ndarrays when possible. |
| 3037 | + if isinstance(values, ABCPandasArray): |
| 3038 | + values = values.to_numpy() |
| 3039 | + if isinstance(dtype, PandasDtype): |
| 3040 | + dtype = dtype.numpy_dtype |
| 3041 | + |
3044 | 3042 | if fastpath is not None:
|
3045 | 3043 | # GH#19265 pyarrow is passing this
|
3046 | 3044 | warnings.warn("fastpath argument is deprecated, will be removed "
|
|
0 commit comments