Skip to content

Commit

Permalink
Move StringArrayTypes to pygmt/_typing.py
Browse files Browse the repository at this point in the history
Also improve docstring of strings_to_ctypes_array function to mention np.ndarray as supported input.
  • Loading branch information
weiji14 committed Oct 11, 2024
1 parent 757da24 commit 17c1e9c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
10 changes: 10 additions & 0 deletions pygmt/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
Type aliases for type hints.
"""

import contextlib
import importlib
from collections.abc import Sequence
from typing import Literal

import numpy as np

# Anchor codes
AnchorCode = Literal["TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"]

# String array types
StringArrayTypes = Sequence[str] | np.ndarray
with contextlib.suppress(ImportError):
StringArrayTypes |= importlib.import_module(name="pyarrow").StringArray
16 changes: 4 additions & 12 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@
import numpy as np
import pandas as pd
from packaging.version import Version
from pygmt._typing import StringArrayTypes
from pygmt.exceptions import GMTInvalidInput

StringArrayTypes = Sequence[str] | np.ndarray

try:
import pyarrow as pa

StringArrayTypes |= pa.StringArray
except ImportError:
pa = None


def dataarray_to_matrix(grid):
"""
Expand Down Expand Up @@ -292,13 +284,13 @@ def sequence_to_ctypes_array(

def strings_to_ctypes_array(strings: StringArrayTypes) -> ctp.Array:
"""
Convert a sequence (e.g., a list) of strings or a pyarrow.StringArray into a ctypes
array.
Convert a sequence (e.g., a list) or numpy.ndarray of strings or a
pyarrow.StringArray into a ctypes array.
Parameters
----------
strings
A sequence of strings or a pyarrow.StringArray.
A sequence of strings, a numpy.ndarray of str dtype, or a pyarrow.StringArray.
Returns
-------
Expand Down
11 changes: 1 addition & 10 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np
import pandas as pd
import xarray as xr
from pygmt._typing import StringArrayTypes
from pygmt.clib.conversion import (
array_to_datetime,
dataarray_to_matrix,
Expand All @@ -34,16 +35,6 @@
tempfile_from_image,
)

StringArrayTypes = Sequence[str] | np.ndarray

try:
import pyarrow as pa

StringArrayTypes |= pa.StringArray
except ImportError:
pa = None


FAMILIES = [
"GMT_IS_DATASET", # Entity is a data table
"GMT_IS_GRID", # Entity is a grid
Expand Down

0 comments on commit 17c1e9c

Please sign in to comment.