From 87583dc82c3d29ade720b78ccea0e8cad99abe66 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 9 Feb 2018 07:12:13 -0600 Subject: [PATCH] Moved --- pandas/core/arrays/base.py | 11 +---------- pandas/tests/extension/__init__.py | 0 .../test_common.py | 18 ++++++++++-------- 3 files changed, 11 insertions(+), 18 deletions(-) create mode 100644 pandas/tests/extension/__init__.py rename pandas/tests/{extension_arrays => extension}/test_common.py (64%) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 8c3d033dffba7..553e1e0ac2066 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -143,8 +143,6 @@ def nbytes(self): def astype(self, dtype, copy=True): """Cast to a NumPy array with 'dtype'. - The default implementation only allows casting to 'object' dtype. - Parameters ---------- dtype : str or dtype @@ -159,14 +157,7 @@ def astype(self, dtype, copy=True): array : ndarray NumPy ndarray with 'dtype' for its dtype. """ - np_dtype = np.dtype(dtype) - - if np_dtype != 'object': - msg = ("{} can only be coerced to 'object' dtype, " - "not '{}'.").format(type(self).__name__, dtype) - raise ValueError(msg) - - return np.array(self, dtype=np_dtype, copy=copy) + return np.array(self, dtype=dtype, copy=copy) def isna(self): # type: () -> np.ndarray diff --git a/pandas/tests/extension/__init__.py b/pandas/tests/extension/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/pandas/tests/extension_arrays/test_common.py b/pandas/tests/extension/test_common.py similarity index 64% rename from pandas/tests/extension_arrays/test_common.py rename to pandas/tests/extension/test_common.py index f19754482b04f..4668154122e45 100644 --- a/pandas/tests/extension_arrays/test_common.py +++ b/pandas/tests/extension/test_common.py @@ -12,6 +12,10 @@ def __init__(self, data): def __array__(self, dtype): return self.data + @property + def dtype(self): + return self.data.dtype + def test_astype(): arr = DummyArray(np.array([1, 2, 3])) @@ -24,13 +28,11 @@ def test_astype(): tm.assert_numpy_array_equal(result, expected) -def test_astype_raises(): - arr = DummyArray(np.array([1, 2, 3])) +def test_astype_no_copy(): + arr = DummyArray(np.array([1, 2, 3], dtype=np.int64)) + result = arr.astype(arr.dtype, copy=False) - # type int for py2 - # class int for py3 - xpr = ("DummyArray can only be coerced to 'object' dtype, not " - "'<.* 'int'>'") + assert arr.data is result - with tm.assert_raises_regex(ValueError, xpr): - arr.astype(int) + result = arr.astype(arr.dtype) + assert arr.data is not result