Skip to content

Commit

Permalink
TEST: added explicit test for np.array(Array)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdementen committed Jul 5, 2024
1 parent 5d93fcb commit 4414bb9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions larray/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
import numpy as np
from numpy.lib import NumpyVersion
import pandas as pd
try:
import xlwings as xw
Expand All @@ -27,6 +28,7 @@
from larray import Array, isnan, asarray, Metadata


NUMPY2 = NumpyVersion(np.__version__) >= '2.0.0'
SKIP_EXCEL_TESTS = False
TESTDATADIR = Path(__file__).parent

Expand Down
28 changes: 26 additions & 2 deletions larray/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

from larray.tests.common import (meta, inputpath,
assert_larray_equal, assert_larray_nan_equal, assert_larray_equiv,
needs_xlwings, needs_pytables, needs_xlsxwriter, needs_openpyxl, must_warn, must_raise,
assert_nparray_equal, assert_nparray_nan_equal)
assert_nparray_equal, assert_nparray_nan_equal,
needs_xlwings, needs_pytables, needs_xlsxwriter, needs_openpyxl, NUMPY2,
must_warn, must_raise)
from larray import (Array, LArray, Axis, AxisCollection, LGroup, IGroup, Metadata,
zeros, zeros_like, ndtest, empty, ones, full, eye, diag, stack, sequence,
asarray, union, clip, exp, where, X, mean, inf, nan, isnan, round,
Expand Down Expand Up @@ -5847,6 +5848,29 @@ def test_growth_rate():
assert_larray_equal(res, expected_res)


def test_np_array():
arr = ndtest((2, 3))
res = np.array(arr) # copy=True by default
assert_nparray_equal(res, arr.data)
assert res is not arr.data

res = np.array(arr, copy=False)
assert res is arr.data

res = np.array(arr, copy=False, dtype=int)
assert res is arr.data

if NUMPY2:
with must_raise(ValueError, match="Unable to avoid copy while creating an array as requested.*"):
# fails to avoid a copy because of the different dtype
res = np.array(arr, copy=False, dtype=float)
else:
# copies anyway because of the different dtype
res = np.array(arr, copy=False, dtype=float)
assert_nparray_equal(res, arr.data)
assert res is not arr.data


if __name__ == "__main__":
# import doctest
# import unittest
Expand Down

0 comments on commit 4414bb9

Please sign in to comment.