diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index f8f6b9d6bb0..ccb22dbe5a2 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -45,7 +45,7 @@ jobs: # Install Micromamba with conda-forge dependencies - name: Setup Micromamba - uses: mamba-org/setup-micromamba@v2.0.0 + uses: mamba-org/setup-micromamba@v2.0.1 with: environment-name: pygmt condarc: | diff --git a/.github/workflows/cache_data.yaml b/.github/workflows/cache_data.yaml index 0c61d35b15c..e9b829dc4a2 100644 --- a/.github/workflows/cache_data.yaml +++ b/.github/workflows/cache_data.yaml @@ -43,7 +43,7 @@ jobs: # Install Micromamba with conda-forge dependencies - name: Setup Micromamba - uses: mamba-org/setup-micromamba@v2.0.0 + uses: mamba-org/setup-micromamba@v2.0.1 with: environment-name: pygmt condarc: | diff --git a/.github/workflows/ci_docs.yml b/.github/workflows/ci_docs.yml index 69987e9522e..4e85e10dae5 100644 --- a/.github/workflows/ci_docs.yml +++ b/.github/workflows/ci_docs.yml @@ -80,7 +80,7 @@ jobs: # Install Micromamba with conda-forge dependencies - name: Setup Micromamba - uses: mamba-org/setup-micromamba@v2.0.0 + uses: mamba-org/setup-micromamba@v2.0.1 with: environment-name: pygmt condarc: | diff --git a/.github/workflows/ci_doctests.yaml b/.github/workflows/ci_doctests.yaml index 0c5445dfd87..9f9f874b30b 100644 --- a/.github/workflows/ci_doctests.yaml +++ b/.github/workflows/ci_doctests.yaml @@ -42,7 +42,7 @@ jobs: # Install Micromamba with conda-forge dependencies - name: Setup Micromamba - uses: mamba-org/setup-micromamba@v2.0.0 + uses: mamba-org/setup-micromamba@v2.0.1 with: environment-name: pygmt condarc: | diff --git a/.github/workflows/ci_tests.yaml b/.github/workflows/ci_tests.yaml index fb002c0d145..68571f14135 100644 --- a/.github/workflows/ci_tests.yaml +++ b/.github/workflows/ci_tests.yaml @@ -114,7 +114,7 @@ jobs: # Install Micromamba with conda-forge dependencies - name: Setup Micromamba - uses: mamba-org/setup-micromamba@v2.0.0 + uses: mamba-org/setup-micromamba@v2.0.1 with: environment-name: pygmt condarc: | diff --git a/.github/workflows/ci_tests_dev.yaml b/.github/workflows/ci_tests_dev.yaml index c9c978e64b9..4491a2ec903 100644 --- a/.github/workflows/ci_tests_dev.yaml +++ b/.github/workflows/ci_tests_dev.yaml @@ -57,7 +57,7 @@ jobs: # Install Micromamba with conda-forge dependencies - name: Setup Micromamba - uses: mamba-org/setup-micromamba@v2.0.0 + uses: mamba-org/setup-micromamba@v2.0.1 with: environment-name: pygmt condarc: | diff --git a/.github/workflows/ci_tests_legacy.yaml b/.github/workflows/ci_tests_legacy.yaml index 4b396125afd..5ac5dcedbbc 100644 --- a/.github/workflows/ci_tests_legacy.yaml +++ b/.github/workflows/ci_tests_legacy.yaml @@ -51,7 +51,7 @@ jobs: # Install Micromamba with conda-forge dependencies - name: Setup Micromamba - uses: mamba-org/setup-micromamba@v2.0.0 + uses: mamba-org/setup-micromamba@v2.0.1 with: environment-name: pygmt condarc: | diff --git a/.github/workflows/publish-to-pypi.yml b/.github/workflows/publish-to-pypi.yml index b04619fccf8..b9994d873e9 100644 --- a/.github/workflows/publish-to-pypi.yml +++ b/.github/workflows/publish-to-pypi.yml @@ -75,10 +75,10 @@ jobs: ls -lh dist/ - name: Publish to Test PyPI - uses: pypa/gh-action-pypi-publish@v1.12.0 + uses: pypa/gh-action-pypi-publish@v1.12.2 with: repository-url: https://test.pypi.org/legacy/ - name: Publish to PyPI if: startsWith(github.ref, 'refs/tags') - uses: pypa/gh-action-pypi-publish@v1.12.0 + uses: pypa/gh-action-pypi-publish@v1.12.2 diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index af8eb3458d4..59fa0d584cd 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -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 ( diff --git a/pygmt/tests/test_clib_to_numpy.py b/pygmt/tests/test_clib_to_numpy.py index 3624ed2be8d..a2f8ba4a2aa 100644 --- a/pygmt/tests/test_clib_to_numpy.py +++ b/pygmt/tests/test_clib_to_numpy.py @@ -3,6 +3,7 @@ """ import sys +from datetime import date, datetime import numpy as np import numpy.testing as npt @@ -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) @@ -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. # @@ -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. # @@ -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: # @@ -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), + )