Skip to content

Commit cb740ed

Browse files
committed
Merge remote-tracking branch 'upstream/master' into pandas-array-upstream+fu1
2 parents d671259 + 6485a36 commit cb740ed

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

pandas/core/arrays/base.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ def nbytes(self):
180180
def astype(self, dtype, copy=True):
181181
"""Cast to a NumPy array with 'dtype'.
182182
183-
The default implementation only allows casting to 'object' dtype.
184-
185183
Parameters
186184
----------
187185
dtype : str or dtype
@@ -196,14 +194,7 @@ def astype(self, dtype, copy=True):
196194
array : ndarray
197195
NumPy ndarray with 'dtype' for its dtype.
198196
"""
199-
np_dtype = np.dtype(dtype)
200-
201-
if np_dtype != 'object':
202-
msg = ("{} can only be coerced to 'object' dtype, "
203-
"not '{}'.").format(type(self).__name__, dtype)
204-
raise ValueError(msg)
205-
206-
return np.array(self, dtype=np_dtype, copy=copy)
197+
return np.array(self, dtype=dtype, copy=copy)
207198

208199
def isna(self):
209200
# type: () -> np.ndarray

pandas/tests/extension/test_common.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)