|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +import pandas.util.testing as tm |
| 6 | +from pandas.core.arrays import ExtensionArray |
| 7 | +from pandas.core.dtypes.common import is_extension_array_dtype |
| 8 | +from pandas.core.dtypes.dtypes import ExtensionDtype |
| 9 | + |
| 10 | + |
| 11 | +class DummyDtype(ExtensionDtype): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class DummyArray(ExtensionArray): |
| 16 | + |
| 17 | + def __init__(self, data): |
| 18 | + self.data = data |
| 19 | + |
| 20 | + def __array__(self, dtype): |
| 21 | + return self.data |
| 22 | + |
| 23 | + @property |
| 24 | + def dtype(self): |
| 25 | + return self.data.dtype |
| 26 | + |
| 27 | + |
| 28 | +class TestExtensionArrayDtype(object): |
| 29 | + |
| 30 | + @pytest.mark.parametrize('values', [ |
| 31 | + pd.Categorical([]), |
| 32 | + pd.Categorical([]).dtype, |
| 33 | + pd.Series(pd.Categorical([])), |
| 34 | + DummyDtype(), |
| 35 | + DummyArray(np.array([1, 2])), |
| 36 | + ]) |
| 37 | + def test_is_extension_array_dtype(self, values): |
| 38 | + assert is_extension_array_dtype(values) |
| 39 | + |
| 40 | + @pytest.mark.parametrize('values', [ |
| 41 | + np.array([]), |
| 42 | + pd.Series(np.array([])), |
| 43 | + ]) |
| 44 | + def test_is_not_extension_array_dtype(self, values): |
| 45 | + assert not is_extension_array_dtype(values) |
| 46 | + |
| 47 | + |
| 48 | +def test_astype(): |
| 49 | + |
| 50 | + arr = DummyArray(np.array([1, 2, 3])) |
| 51 | + expected = np.array([1, 2, 3], dtype=object) |
| 52 | + |
| 53 | + result = arr.astype(object) |
| 54 | + tm.assert_numpy_array_equal(result, expected) |
| 55 | + |
| 56 | + result = arr.astype('object') |
| 57 | + tm.assert_numpy_array_equal(result, expected) |
| 58 | + |
| 59 | + |
| 60 | +def test_astype_no_copy(): |
| 61 | + arr = DummyArray(np.array([1, 2, 3], dtype=np.int64)) |
| 62 | + result = arr.astype(arr.dtype, copy=False) |
| 63 | + |
| 64 | + assert arr.data is result |
| 65 | + |
| 66 | + result = arr.astype(arr.dtype) |
| 67 | + assert arr.data is not result |
0 commit comments