Skip to content

Commit

Permalink
Merge branch 'main' into feature/scatter
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Nov 14, 2024
2 parents 5242e0a + 66b22dc commit 4d450cb
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

# Install Micromamba with conda-forge dependencies
- name: Setup Micromamba
uses: mamba-org/[email protected].0
uses: mamba-org/[email protected].1
with:
environment-name: pygmt
condarc: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cache_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:

# Install Micromamba with conda-forge dependencies
- name: Setup Micromamba
uses: mamba-org/[email protected].0
uses: mamba-org/[email protected].1
with:
environment-name: pygmt
condarc: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:

# Install Micromamba with conda-forge dependencies
- name: Setup Micromamba
uses: mamba-org/[email protected].0
uses: mamba-org/[email protected].1
with:
environment-name: pygmt
condarc: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_doctests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

# Install Micromamba with conda-forge dependencies
- name: Setup Micromamba
uses: mamba-org/[email protected].0
uses: mamba-org/[email protected].1
with:
environment-name: pygmt
condarc: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:

# Install Micromamba with conda-forge dependencies
- name: Setup Micromamba
uses: mamba-org/[email protected].0
uses: mamba-org/[email protected].1
with:
environment-name: pygmt
condarc: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_tests_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:

# Install Micromamba with conda-forge dependencies
- name: Setup Micromamba
uses: mamba-org/[email protected].0
uses: mamba-org/[email protected].1
with:
environment-name: pygmt
condarc: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_tests_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:

# Install Micromamba with conda-forge dependencies
- name: Setup Micromamba
uses: mamba-org/[email protected].0
uses: mamba-org/[email protected].1
with:
environment-name: pygmt
condarc: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ jobs:
ls -lh dist/
- name: Publish to Test PyPI
uses: pypa/[email protected].0
uses: pypa/[email protected].2
with:
repository-url: https://test.pypi.org/legacy/

- name: Publish to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/[email protected].0
uses: pypa/[email protected].2
6 changes: 3 additions & 3 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ def _to_numpy(data: Any) -> np.ndarray:
The C contiguous NumPy array.
"""
# Mapping of unsupported dtypes to the expected NumPy dtype.
dtypes: dict[str, type] = {
"date32[day][pyarrow]": np.datetime64,
"date64[ms][pyarrow]": np.datetime64,
dtypes: dict[str, str | type] = {
"date32[day][pyarrow]": "datetime64[D]",
"date64[ms][pyarrow]": "datetime64[ms]",
}

if (
Expand Down
74 changes: 72 additions & 2 deletions pygmt/tests/test_clib_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import sys
from datetime import date, datetime

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -48,11 +49,12 @@ def _check_result(result, expected_dtype):
np.complex128,
id="complex",
),
pytest.param(["abc", "defg", "12345"], np.str_, id="string"),
],
)
def test_to_numpy_python_types_numeric(data, expected_dtype):
def test_to_numpy_python_types(data, expected_dtype):
"""
Test the _to_numpy function with Python built-in numeric types.
Test the _to_numpy function with Python built-in types.
"""
result = _to_numpy(data)
_check_result(result, expected_dtype)
Expand Down Expand Up @@ -121,6 +123,17 @@ def test_to_numpy_ndarray_numpy_dtypes_numeric(dtype, expected_dtype):
npt.assert_array_equal(result, array, strict=True)


@pytest.mark.parametrize("dtype", [None, np.str_, "U10"])
def test_to_numpy_ndarray_numpy_dtypes_string(dtype):
"""
Test the _to_numpy function with NumPy arrays of string types.
"""
array = np.array(["abc", "defg", "12345"], dtype=dtype)
result = _to_numpy(array)
_check_result(result, np.str_)
npt.assert_array_equal(result, array)


########################################################################################
# Test the _to_numpy function with pandas.Series.
#
Expand Down Expand Up @@ -161,6 +174,28 @@ def test_to_numpy_pandas_series_numpy_dtypes_numeric(dtype, expected_dtype):
npt.assert_array_equal(result, series)


@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed")
@pytest.mark.parametrize(
("dtype", "expected_dtype"),
[
pytest.param("date32[day][pyarrow]", "datetime64[D]", id="date32[day]"),
pytest.param("date64[ms][pyarrow]", "datetime64[ms]", id="date64[ms]"),
],
)
def test_to_numpy_pandas_series_pyarrow_dtypes_date(dtype, expected_dtype):
"""
Test the _to_numpy function with pandas.Series of PyArrow date32/date64 types.
"""
series = pd.Series(pd.date_range(start="2024-01-01", periods=3), dtype=dtype)
result = _to_numpy(series)
_check_result(result, np.datetime64)
assert result.dtype == expected_dtype # Explicitly check the date unit.
npt.assert_array_equal(
result,
np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=expected_dtype),
)


########################################################################################
# Test the _to_numpy function with PyArrow arrays.
#
Expand All @@ -170,6 +205,9 @@ def test_to_numpy_pandas_series_numpy_dtypes_numeric(dtype, expected_dtype):
# - int8, int16, int32, int64
# - uint8, uint16, uint32, uint64
# - float16, float32, float64
# - Date types:
# - date32[day]
# - date64[ms]
#
# In PyArrow, array types can be specified in two ways:
#
Expand Down Expand Up @@ -238,3 +276,35 @@ def test_to_numpy_pyarrow_array_pyarrow_dtypes_numeric_with_na(dtype, expected_d
result = _to_numpy(array)
_check_result(result, expected_dtype)
npt.assert_array_equal(result, array)


@pytest.mark.skipif(not _HAS_PYARROW, reason="pyarrow is not installed")
@pytest.mark.parametrize(
("dtype", "expected_dtype"),
[
pytest.param("date32[day]", "datetime64[D]", id="date32[day]"),
pytest.param("date64[ms]", "datetime64[ms]", id="date64[ms]"),
],
)
def test_to_numpy_pyarrow_array_pyarrow_dtypes_date(dtype, expected_dtype):
"""
Test the _to_numpy function with PyArrow arrays of PyArrow date types.
date32[day] and date64[ms] are stored as 32-bit and 64-bit integers, respectively,
representing the number of days and milliseconds since the UNIX epoch (1970-01-01).
Here we explicitly check the dtype and date unit of the result.
"""
data = [
date(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 3),
]
array = pa.array(data, type=dtype)
result = _to_numpy(array)
_check_result(result, np.datetime64)
assert result.dtype == expected_dtype # Explicitly check the date unit.
npt.assert_array_equal(
result,
np.array(["2024-01-01", "2024-01-02", "2024-01-03"], dtype=expected_dtype),
)

0 comments on commit 4d450cb

Please sign in to comment.