Skip to content

Commit cac2a8b

Browse files
committed
Added constructor and setitem and dtype tests
1 parent 7e3ff59 commit cac2a8b

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

pandas/core/arrays/numpy_.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def __init__(self, dtype):
3838
self._name = dtype.name
3939
self._type = dtype.type
4040

41+
@property
42+
def numpy_dtype(self):
43+
return self._dtype
44+
4145
@property
4246
def name(self):
4347
return self._name
@@ -98,6 +102,8 @@ class PandasArray(ExtensionArray, ExtensionOpsMixin, NDArrayOperatorsMixin):
98102
----------
99103
values : ndarray
100104
The NumPy ndarray to wrap. Must be 1-dimensional.
105+
copy : bool, default False
106+
Whether to copy `values`.
101107
102108
Notes
103109
-----
@@ -113,7 +119,7 @@ class PandasArray(ExtensionArray, ExtensionOpsMixin, NDArrayOperatorsMixin):
113119
# ------------------------------------------------------------------------
114120
# Constructors
115121

116-
def __init__(self, values):
122+
def __init__(self, values, copy=False):
117123
if isinstance(values, type(self)):
118124
values = values._ndarray
119125
if not isinstance(values, np.ndarray):
@@ -122,6 +128,9 @@ def __init__(self, values):
122128
if values.ndim != 1:
123129
raise ValueError("PandasArray must be 1-dimensional.")
124130

131+
if copy:
132+
values = values.copy()
133+
125134
self._ndarray = values
126135
self._dtype = PandasDtype(values.dtype)
127136

pandas/tests/arrays/test_numpy.py

+58-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_to_numpy():
2626
tm.assert_numpy_array_equal(result, expected)
2727

2828

29-
def test_setitem():
29+
def test_setitem_series():
3030
ser = pd.Series([1, 2, 3])
3131
ser.array[0] = 10
3232
expected = pd.Series([10, 2, 3])
@@ -99,6 +99,46 @@ def test_series_constructor_with_astype():
9999
tm.assert_series_equal(result, expected)
100100

101101

102+
@pytest.fixture(params=[
103+
np.array(['a', 'b'], dtype=object),
104+
np.array([0, 1], dtype=float),
105+
np.array([0, 1], dtype=int),
106+
np.array([0, 1+2j], dtype=complex),
107+
np.array([True, False], dtype=bool),
108+
np.array([0, 1], dtype='datetime64[ns]'),
109+
np.array([0, 1], dtype='timedelta64[ns]'),
110+
])
111+
def any_numpy_array(request):
112+
"""Parametrized fixture for NumPy arrays with different dtypes.
113+
114+
This excludes string and bytes.
115+
"""
116+
return request.param
117+
118+
119+
def test_constructor_copy():
120+
arr = np.array([0, 1])
121+
result = PandasArray(arr, copy=True)
122+
123+
assert np.shares_memory(result._ndarray, arr) is False
124+
125+
126+
def test_constructor_with_data(any_numpy_array):
127+
nparr = any_numpy_array
128+
arr = PandasArray(nparr)
129+
assert arr.dtype.numpy_dtype == nparr.dtype
130+
131+
132+
def test_setitem(any_numpy_array):
133+
nparr = any_numpy_array
134+
arr = PandasArray(nparr, copy=True)
135+
136+
arr[0] = arr[1]
137+
nparr[0] = nparr[1]
138+
139+
tm.assert_numpy_array_equal(arr.to_numpy(), nparr)
140+
141+
102142
@pytest.mark.parametrize('dtype, expected', [
103143
('bool', True),
104144
('int', True),
@@ -114,3 +154,20 @@ def test_series_constructor_with_astype():
114154
def test_is_numeric(dtype, expected):
115155
dtype = PandasDtype(dtype)
116156
assert dtype._is_numeric is expected
157+
158+
159+
@pytest.mark.parametrize('dtype, expected', [
160+
('bool', True),
161+
('int', False),
162+
('uint', False),
163+
('float', False),
164+
('complex', False),
165+
('str', False),
166+
('bytes', False),
167+
('datetime64[ns]', False),
168+
('object', False),
169+
('void', False)
170+
])
171+
def test_is_boolean(dtype, expected):
172+
dtype = PandasDtype(dtype)
173+
assert dtype._is_boolean is expected

0 commit comments

Comments
 (0)