|
1 | 1 | import numbers
|
2 |
| -from typing import TYPE_CHECKING, Tuple, Type, Union |
| 2 | +from typing import TYPE_CHECKING, Dict, Tuple, Type, Union |
3 | 3 | import warnings
|
4 | 4 |
|
5 | 5 | import numpy as np
|
@@ -449,17 +449,20 @@ def astype(self, dtype, copy: bool = True) -> ArrayLike:
|
449 | 449 | if incompatible type with an IntegerDtype, equivalent of same_kind
|
450 | 450 | casting
|
451 | 451 | """
|
452 |
| - from pandas.core.arrays.boolean import BooleanArray, BooleanDtype |
| 452 | + from pandas.core.arrays.boolean import BooleanDtype |
| 453 | + from pandas.core.arrays.string_ import StringDtype |
453 | 454 |
|
454 | 455 | dtype = pandas_dtype(dtype)
|
455 | 456 |
|
456 | 457 | # if we are astyping to an existing IntegerDtype we can fastpath
|
457 | 458 | if isinstance(dtype, _IntegerDtype):
|
458 | 459 | result = self._data.astype(dtype.numpy_dtype, copy=False)
|
459 |
| - return type(self)(result, mask=self._mask, copy=False) |
| 460 | + return dtype.construct_array_type()(result, mask=self._mask, copy=False) |
460 | 461 | elif isinstance(dtype, BooleanDtype):
|
461 | 462 | result = self._data.astype("bool", copy=False)
|
462 |
| - return BooleanArray(result, mask=self._mask, copy=False) |
| 463 | + return dtype.construct_array_type()(result, mask=self._mask, copy=False) |
| 464 | + elif isinstance(dtype, StringDtype): |
| 465 | + return dtype.construct_array_type()._from_sequence(self, copy=False) |
463 | 466 |
|
464 | 467 | # coerce
|
465 | 468 | if is_float_dtype(dtype):
|
@@ -748,7 +751,7 @@ class UInt64Dtype(_IntegerDtype):
|
748 | 751 | __doc__ = _dtype_docstring.format(dtype="uint64")
|
749 | 752 |
|
750 | 753 |
|
751 |
| -_dtypes = { |
| 754 | +_dtypes: Dict[str, _IntegerDtype] = { |
752 | 755 | "int8": Int8Dtype(),
|
753 | 756 | "int16": Int16Dtype(),
|
754 | 757 | "int32": Int32Dtype(),
|
|
0 commit comments