|
| 1 | +""" |
| 2 | +Test the functions in the clib.conversion module. |
| 3 | +""" |
| 4 | + |
| 5 | +import datetime |
| 6 | +import importlib |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import numpy.testing as npt |
| 10 | +import pandas as pd |
| 11 | +import pytest |
| 12 | +from pygmt.clib.conversion import vectors_to_arrays |
| 13 | + |
| 14 | +try: |
| 15 | + importlib.util.find_spec("pyarrow") |
| 16 | + _HAS_PYARROW = True |
| 17 | +except ImportError: |
| 18 | + _HAS_PYARROW = False |
| 19 | + |
| 20 | + |
| 21 | +def _check_arrays(arrays): |
| 22 | + """ |
| 23 | + A helper function to check the results of vectors_to_arrays. |
| 24 | +
|
| 25 | + - Check if all arrays are C-contiguous |
| 26 | + - Check if all arrays are numpy arrays |
| 27 | + - Check if all arrays are 1-D |
| 28 | + """ |
| 29 | + # Check if all arrays are C-contiguous |
| 30 | + assert all(i.flags.c_contiguous for i in arrays) |
| 31 | + # Check if all arrays are numpy arrays |
| 32 | + assert all(isinstance(i, np.ndarray) for i in arrays) |
| 33 | + # Check if all arrays are 1-D |
| 34 | + assert all(i.ndim == 1 for i in arrays) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + "vectors", |
| 39 | + [ |
| 40 | + pytest.param([[1, 2], (3, 4), range(5, 7)], id="python_objects"), |
| 41 | + pytest.param( |
| 42 | + [np.array([1, 2]), np.array([3, 4]), np.array(range(5, 7))], |
| 43 | + id="numpy_arrays", |
| 44 | + ), |
| 45 | + pytest.param([[1, 2], np.array([3, 4]), range(5, 7)], id="mixed"), |
| 46 | + pytest.param([1, 2, 3.0], id="scalars"), |
| 47 | + ], |
| 48 | +) |
| 49 | +def test_vectors_to_arrays(vectors): |
| 50 | + """ |
| 51 | + Test the vectors_to_arrays function for various input types. |
| 52 | + """ |
| 53 | + arrays = vectors_to_arrays(vectors) |
| 54 | + _check_arrays(arrays) |
| 55 | + |
| 56 | + |
| 57 | +def test_vectors_to_arrays_not_c_contiguous(): |
| 58 | + """ |
| 59 | + Test the vectors_to_arrays function with numpy arrays that are not C-contiguous. |
| 60 | + """ |
| 61 | + data = np.array([[1, 2], [3, 4], [5, 6]]) |
| 62 | + vectors = [data[:, 0], data[:, 1]] |
| 63 | + assert all(not i.flags.c_contiguous for i in vectors) |
| 64 | + arrays = vectors_to_arrays(vectors) |
| 65 | + _check_arrays(arrays) |
| 66 | + |
| 67 | + |
| 68 | +def test_vectors_to_arrays_pandas_nan(): |
| 69 | + """ |
| 70 | + Test the vectors_to_arrays function with pandas Series containing NaNs. |
| 71 | + """ |
| 72 | + vectors = [pd.Series(data=[0, 4, pd.NA, 8, 6], dtype=pd.Int32Dtype())] |
| 73 | + arrays = vectors_to_arrays(vectors) |
| 74 | + npt.assert_equal(arrays[0], np.array([0, 4, np.nan, 8, 6], dtype=np.float64)) |
| 75 | + _check_arrays(arrays) |
| 76 | + |
| 77 | + |
| 78 | +def test_vectors_to_arrays_pandas_string(): |
| 79 | + """ |
| 80 | + Test the vectors_to_arrays function with pandas Series containing datetime64. |
| 81 | + """ |
| 82 | + vectors = [ |
| 83 | + pd.Series(["abc", "defhig"]), |
| 84 | + pd.Series(["abcdef", "123456"], dtype="string"), |
| 85 | + ] |
| 86 | + arrays = vectors_to_arrays(vectors) |
| 87 | + assert all(isinstance(i.dtype, np.dtypes.StrDType) for i in arrays) |
| 88 | + _check_arrays(arrays) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed.") |
| 92 | +def test_vectors_to_arrays_pyarrow_datetime(): |
| 93 | + """ |
| 94 | + Test the vectors_to_arrays function with pyarrow arrays containing datetime64. |
| 95 | + """ |
| 96 | + vectors = [ |
| 97 | + pd.Series( |
| 98 | + data=[datetime.date(2020, 1, 1), datetime.date(2021, 12, 31)], |
| 99 | + dtype="date32[day][pyarrow]", |
| 100 | + ), |
| 101 | + pd.Series( |
| 102 | + data=[datetime.date(2022, 1, 1), datetime.date(2023, 12, 31)], |
| 103 | + dtype="date64[ms][pyarrow]", |
| 104 | + ), |
| 105 | + ] |
| 106 | + arrays = vectors_to_arrays(vectors) |
| 107 | + assert all(isinstance(i.dtype, np.dtypes.DateTime64DType) for i in arrays) |
| 108 | + _check_arrays(arrays) |
0 commit comments