From 5af79f8a8b5e363517b4b5c7fe9624ce5b828b6b Mon Sep 17 00:00:00 2001 From: ajpotts Date: Wed, 9 Oct 2024 11:51:25 -0400 Subject: [PATCH] Closes #3714: pdarray.shape should be a tuple (#3803) Co-authored-by: Amanda Potts --- arkouda/pdarrayclass.py | 16 ++++++++++++++-- tests/pdarray_creation_test.py | 16 ++++++++-------- tests/pdarrayclass_test.py | 18 ++++++++++++++++-- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/arkouda/pdarrayclass.py b/arkouda/pdarrayclass.py index 2cbed3f396..fe9d239d40 100644 --- a/arkouda/pdarrayclass.py +++ b/arkouda/pdarrayclass.py @@ -364,7 +364,7 @@ def __init__( self.dtype = dtype(mydtype) self.size = size self.ndim = ndim - self.shape = shape + self._shape = tuple(shape) self.itemsize = itemsize if max_bits: self.max_bits = max_bits @@ -399,6 +399,18 @@ def __repr__(self): return generic_msg(cmd="repr", args={"array": self, "printThresh": pdarrayIterThresh}) + @property + def shape(self): + """ + Return the shape of an array. + + Returns + ------- + tuple of int + The elements of the shape tuple give the lengths of the corresponding array dimensions. + """ + return tuple(self._shape) + @property def max_bits(self): if self.dtype == bigint: @@ -962,7 +974,7 @@ def __getitem__(self, key): # use 'None' values in the original key to expand the dimensions shape = [] - rs = ret_array.shape + rs = list(ret_array.shape) for k in key_with_none: if k is None: shape.append(1) diff --git a/tests/pdarray_creation_test.py b/tests/pdarray_creation_test.py index 82339d8ea7..4d7c4578e9 100644 --- a/tests/pdarray_creation_test.py +++ b/tests/pdarray_creation_test.py @@ -53,7 +53,7 @@ def test_array_creation_multi_dim(self, size, dtype): ak.array(np.ones(shape), dtype), ]: assert isinstance(pda, ak.pdarray) - assert pda.shape == list(shape) + assert pda.shape == shape assert dtype == pda.dtype @pytest.mark.skip_if_max_rank_greater_than(3) @@ -248,7 +248,7 @@ def test_randint_array_dtype(self, size, array_type): assert isinstance(test_array, ak.pdarray) assert size == len(test_array) assert array_type == test_array.dtype - assert [size] == test_array.shape + assert (size,) == test_array.shape assert ((0 <= test_array) & (test_array <= size)).all() # (The above function tests randint with various ARRAY dtypes; the function below @@ -259,7 +259,7 @@ def test_randint_num_dtype(self, dtype): assert isinstance(test_array, ak.pdarray) assert 1000 == len(test_array) assert ak.int64 == test_array.dtype - assert [1000] == test_array.shape + assert (1000,) == test_array.shape assert ((0 <= test_array) & (test_array <= 1000)).all() @pytest.mark.parametrize("size", pytest.prob_size) @@ -334,7 +334,7 @@ def test_uniform(self, size): test_array = ak.uniform(size) assert isinstance(test_array, ak.pdarray) assert ak.float64 == test_array.dtype - assert [size] == test_array.shape + assert (size,) == test_array.shape u_array = ak.uniform(size=3, low=0, high=5, seed=0) assert [0.30013431967121934, 0.47383036230759112, 1.0441791878997098] == u_array.to_list() @@ -372,7 +372,7 @@ def test_zeros_dtype_mult_dim(self, size, dtype): zeros = ak.zeros(shape, dtype) assert isinstance(zeros, ak.pdarray) assert dtype == zeros.dtype - assert zeros.shape == list(shape) + assert zeros.shape == shape assert (0 == zeros).all() @pytest.mark.skip_if_max_rank_greater_than(3) @@ -412,7 +412,7 @@ def test_ones_dtype_multi_dim(self, size, dtype): shape = (2, 2, size) ones = ak.ones(shape, dtype) assert isinstance(ones, ak.pdarray) - assert ones.shape == list(shape) + assert ones.shape == shape assert dtype == ones.dtype assert (1 == ones).all() @@ -464,7 +464,7 @@ def test_full_dtype_multi_dim(self, size, dtype): type_full = ak.full(shape, 1, dtype) assert isinstance(type_full, ak.pdarray) assert dtype == type_full.dtype - assert type_full.shape == list(shape) + assert type_full.shape == shape assert (1 == type_full).all() @pytest.mark.skip_if_max_rank_greater_than(3) @@ -737,7 +737,7 @@ def test_random_strings_lognormal_with_seed(self): def test_mulitdimensional_array_creation(self): a = ak.array([[0, 0], [0, 1], [1, 1]]) assert isinstance(a, ak.pdarray) - assert a.shape == [3, 2] + assert a.shape == (3, 2) @pytest.mark.parametrize("size", pytest.prob_size) @pytest.mark.parametrize("dtype", [bool, np.float64, np.int64, str]) diff --git a/tests/pdarrayclass_test.py b/tests/pdarrayclass_test.py index c72c6f26ac..00418da328 100644 --- a/tests/pdarrayclass_test.py +++ b/tests/pdarrayclass_test.py @@ -1,13 +1,27 @@ import pytest import arkouda as ak +import numpy as np class TestPdarrayClass: - @pytest.mark.skip_if_max_rank_less_than(3) + @pytest.mark.skip_if_max_rank_less_than(2) def test_reshape(self): a = ak.arange(4) r = a.reshape((2, 2)) - assert r.shape == [2, 2] + assert r.shape == (2, 2) assert isinstance(r, ak.pdarray) + + def test_shape(self): + a = ak.arange(4) + np_a = np.arange(4) + assert isinstance(a.shape, tuple) + assert a.shape == np_a.shape + + @pytest.mark.skip_if_max_rank_less_than(2) + def test_shape_multidim(self): + a = ak.arange(4).reshape((2,2)) + np_a = np.arange(4).reshape((2,2)) + assert isinstance(a.shape, tuple) + assert a.shape == np_a.shape