Skip to content

Commit

Permalink
Added constructor and setitem and dtype tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Dec 28, 2018
1 parent 7e3ff59 commit cac2a8b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
11 changes: 10 additions & 1 deletion pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def __init__(self, dtype):
self._name = dtype.name
self._type = dtype.type

@property
def numpy_dtype(self):
return self._dtype

@property
def name(self):
return self._name
Expand Down Expand Up @@ -98,6 +102,8 @@ class PandasArray(ExtensionArray, ExtensionOpsMixin, NDArrayOperatorsMixin):
----------
values : ndarray
The NumPy ndarray to wrap. Must be 1-dimensional.
copy : bool, default False
Whether to copy `values`.
Notes
-----
Expand All @@ -113,7 +119,7 @@ class PandasArray(ExtensionArray, ExtensionOpsMixin, NDArrayOperatorsMixin):
# ------------------------------------------------------------------------
# Constructors

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

if copy:
values = values.copy()

self._ndarray = values
self._dtype = PandasDtype(values.dtype)

Expand Down
59 changes: 58 additions & 1 deletion pandas/tests/arrays/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_to_numpy():
tm.assert_numpy_array_equal(result, expected)


def test_setitem():
def test_setitem_series():
ser = pd.Series([1, 2, 3])
ser.array[0] = 10
expected = pd.Series([10, 2, 3])
Expand Down Expand Up @@ -99,6 +99,46 @@ def test_series_constructor_with_astype():
tm.assert_series_equal(result, expected)


@pytest.fixture(params=[
np.array(['a', 'b'], dtype=object),
np.array([0, 1], dtype=float),
np.array([0, 1], dtype=int),
np.array([0, 1+2j], dtype=complex),
np.array([True, False], dtype=bool),
np.array([0, 1], dtype='datetime64[ns]'),
np.array([0, 1], dtype='timedelta64[ns]'),
])
def any_numpy_array(request):
"""Parametrized fixture for NumPy arrays with different dtypes.
This excludes string and bytes.
"""
return request.param


def test_constructor_copy():
arr = np.array([0, 1])
result = PandasArray(arr, copy=True)

assert np.shares_memory(result._ndarray, arr) is False


def test_constructor_with_data(any_numpy_array):
nparr = any_numpy_array
arr = PandasArray(nparr)
assert arr.dtype.numpy_dtype == nparr.dtype


def test_setitem(any_numpy_array):
nparr = any_numpy_array
arr = PandasArray(nparr, copy=True)

arr[0] = arr[1]
nparr[0] = nparr[1]

tm.assert_numpy_array_equal(arr.to_numpy(), nparr)


@pytest.mark.parametrize('dtype, expected', [
('bool', True),
('int', True),
Expand All @@ -114,3 +154,20 @@ def test_series_constructor_with_astype():
def test_is_numeric(dtype, expected):
dtype = PandasDtype(dtype)
assert dtype._is_numeric is expected


@pytest.mark.parametrize('dtype, expected', [
('bool', True),
('int', False),
('uint', False),
('float', False),
('complex', False),
('str', False),
('bytes', False),
('datetime64[ns]', False),
('object', False),
('void', False)
])
def test_is_boolean(dtype, expected):
dtype = PandasDtype(dtype)
assert dtype._is_boolean is expected

0 comments on commit cac2a8b

Please sign in to comment.