Ejemplo n.º 1
0
    def astype(self,
               dtype: Any,
               copy: bool = True,
               casting: str = "unsafe") -> Any:
        _logger.debug("RLEArray.astype(dtype=%r, copy=%r)", dtype, copy)
        if isinstance(dtype, RLEDtype):
            if (not copy) and (dtype == self.dtype):
                return self
            return RLEArray(
                data=self._data.astype(dtype._dtype, casting=casting),
                positions=self._positions.copy(),
            )
        if isinstance(dtype, pd.StringDtype):
            # TODO: fast-path
            return StringArray._from_sequence([str(x) for x in self])

        if casting != "unsafe":
            return np.array(self, copy=copy).astype(dtype=dtype,
                                                    casting=casting)
        else:
            return np.array(self, dtype=dtype, copy=copy)
Ejemplo n.º 2
0
     pd.CategoricalDtype(None, ordered=True),
     pd.Categorical(["a", "b"], ordered=True),
 ),
 # Interval
 (
     [pd.Interval(1, 2), pd.Interval(3, 4)],
     "interval",
     IntervalArray.from_tuples([(1, 2), (3, 4)]),
 ),
 # Sparse
 ([0, 1], "Sparse[int64]", SparseArray([0, 1], dtype="int64")),
 # IntegerNA
 ([1, None], "Int16", integer_array([1, None], dtype="Int16")),
 (pd.Series([1, 2]), None, PandasArray(np.array([1, 2], dtype=np.int64))),
 # String
 (["a", None], "string", StringArray._from_sequence(["a", None])),
 (["a", None], pd.StringDtype(), StringArray._from_sequence(["a", None])),
 # Boolean
 ([True, None], "boolean", BooleanArray._from_sequence([True, None])),
 ([True, None], pd.BooleanDtype(), BooleanArray._from_sequence([True, None])),
 # Index
 (pd.Index([1, 2]), None, PandasArray(np.array([1, 2], dtype=np.int64))),
 # Series[EA] returns the EA
 (
     pd.Series(pd.Categorical(["a", "b"], categories=["a", "b", "c"])),
     None,
     pd.Categorical(["a", "b"], categories=["a", "b", "c"]),
 ),
 # "3rd party" EAs work
 ([decimal.Decimal(0), decimal.Decimal(1)], "decimal", to_decimal([0, 1])),
 # pass an ExtensionArray, but a different dtype