Skip to content

Commit

Permalink
Add tests for pandas.Series with NumPy numeric dtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Nov 6, 2024
1 parent 127d8ca commit 6ae1ddb
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion pygmt/tests/test_clib_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import numpy.testing as npt
import pandas as pd
import pytest
from pygmt.clib.conversion import _to_numpy
from pygmt.clib.session import DTYPES
Expand All @@ -21,7 +22,7 @@ def _check_result(result, supported):


########################################################################################
# Test the _to_numpy function with NumPy dtypes.
# Test the _to_numpy function with NumPy arrays.
#
# There are 24 fundamental dtypes in NumPy. Not all of them are supported by PyGMT.
# Reference: https://numpy.org/doc/2.1/reference/arrays.scalars.html
Expand Down Expand Up @@ -80,3 +81,47 @@ def test_to_numpy_ndarray_numpy_dtypes_numeric(dtype, supported):
result = _to_numpy(array)
_check_result(result, supported)
npt.assert_array_equal(result, array)


########################################################################################
# Test the _to_numpy function with pandas.Series.
#
# In pandas, dtype can be specified by
#
# 1. NumPy dtypes (see above)
# 2. pandas dtypes
# 3. PyArrow dtypes
#
# Reference: https://pandas.pydata.org/docs/reference/arrays.html
########################################################################################
@pytest.mark.parametrize(
("dtype", "supported"),
[
(np.int8, True),
(np.int16, True),
(np.int32, True),
(np.int64, True),
(np.longlong, True),
(np.uint8, True),
(np.uint16, True),
(np.uint32, True),
(np.uint64, True),
(np.ulonglong, True),
(np.float16, False),
(np.float32, True),
(np.float64, True),
(np.longdouble, False),
(np.complex64, False),
(np.complex128, False),
(np.clongdouble, False),
],
)
def test_to_numpy_pandas_series_numpy_dtypes_numeric(dtype, supported):
"""
Test the _to_numpy function with pandas.Series of NumPy numeric dtypes.
"""
series = pd.Series([1, 2, 3], dtype=dtype)
assert series.dtype == dtype
result = _to_numpy(series)
_check_result(result, supported)
npt.assert_array_equal(result, series)

0 comments on commit 6ae1ddb

Please sign in to comment.