Skip to content

Commit f41974d

Browse files
authored
Refactor to specify numpy dtypes in a consistent way (#3577)
1 parent 043a49c commit f41974d

File tree

14 files changed

+33
-30
lines changed

14 files changed

+33
-30
lines changed

examples/gallery/symbols/datetime_inputs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
)
4141

4242
# numpy.datetime64 types
43-
x = np.array(["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], dtype="datetime64")
43+
x = np.array(
44+
["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], dtype=np.datetime64
45+
)
4446
y = [1, 2, 3]
4547
fig.plot(x=x, y=y, style="c0.4c", pen="1p", fill="red3")
4648

examples/tutorials/advanced/date_time_charts.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@
222222
# before passing it as an argument. However, ``np.array`` objects use less memory and
223223
# allow developers to specify data types.
224224

225-
x = np.array(["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], dtype="datetime64")
225+
x = np.array(
226+
["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], dtype=np.datetime64
227+
)
226228
y = [2, 7, 5]
227229

228230
fig = pygmt.Figure()

pygmt/clib/session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,12 +910,12 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int:
910910
Examples
911911
--------
912912
>>> import numpy as np
913-
>>> data = np.array([1, 2, 3], dtype="float64")
913+
>>> data = np.array([1, 2, 3], dtype=np.float64)
914914
>>> with Session() as lib:
915915
... gmttype = lib._check_dtype_and_dim(data, ndim=1)
916916
... gmttype == lib["GMT_DOUBLE"]
917917
True
918-
>>> data = np.ones((5, 2), dtype="float32")
918+
>>> data = np.ones((5, 2), dtype=np.float32)
919919
>>> with Session() as lib:
920920
... gmttype = lib._check_dtype_and_dim(data, ndim=2)
921921
... gmttype == lib["GMT_FLOAT"]
@@ -1505,7 +1505,7 @@ def virtualfile_from_vectors(
15051505
strings = np.array(
15061506
[" ".join(vals) for vals in zip(*string_arrays, strict=True)]
15071507
)
1508-
strings = np.asanyarray(a=strings, dtype=str)
1508+
strings = np.asanyarray(a=strings, dtype=np.str_)
15091509
self.put_strings(
15101510
dataset, family="GMT_IS_VECTOR|GMT_IS_DUPLICATE", strings=strings
15111511
)

pygmt/datatypes/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def to_strings(self) -> np.ndarray[Any, np.dtype[np.str_]]:
168168
)
169169
warnings.warn(msg, category=RuntimeWarning, stacklevel=1)
170170
textvector = [item if item is not None else b"" for item in textvector]
171-
return np.char.decode(textvector) if textvector else np.array([], dtype=str)
171+
return np.char.decode(textvector) if textvector else np.array([], dtype=np.str_)
172172

173173
def to_dataframe(
174174
self,

pygmt/helpers/tempfile.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class GMTTempFile:
5050
... np.savetxt(tmpfile.name, (x, y, z), fmt="%.1f")
5151
... lines = tmpfile.read()
5252
... print(lines)
53-
... nx, ny, nz = tmpfile.loadtxt(unpack=True, dtype=float)
53+
... nx, ny, nz = tmpfile.loadtxt(unpack=True, dtype=np.float64)
5454
... print(nx, ny, nz)
5555
0.0 1.0 2.0
5656
0.0 1.0 2.0
@@ -143,9 +143,7 @@ def tempfile_from_geojson(geojson):
143143
# 32-bit integer overflow issue. Related issues:
144144
# https://github.com/geopandas/geopandas/issues/967#issuecomment-842877704
145145
# https://github.com/GenericMappingTools/pygmt/issues/2497
146-
147146
int32_info = np.iinfo(np.int32)
148-
149147
if Version(gpd.__version__).major < 1: # GeoPandas v0.x
150148
# The default engine 'fiona' supports the 'schema' parameter.
151149
if geojson.index.name is None:

pygmt/src/info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ def info(data, **kwargs):
9898
result = np.loadtxt(result.splitlines())
9999
except ValueError:
100100
# Load non-numerical outputs in str type, e.g. for datetime
101-
result = np.loadtxt(result.splitlines(), dtype="str")
101+
result = np.loadtxt(result.splitlines(), dtype=np.str_)
102102

103103
return result

pygmt/src/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def text_( # noqa: PLR0912
228228
if name == "angle":
229229
extra_arrays.append(arg)
230230
else:
231-
extra_arrays.append(np.asarray(arg, dtype=str))
231+
extra_arrays.append(np.asarray(arg, dtype=np.str_))
232232

233233
# If an array of transparency is given, GMT will read it from the last numerical
234234
# column per data record.
@@ -237,7 +237,7 @@ def text_( # noqa: PLR0912
237237
kwargs["t"] = True
238238

239239
# Append text to the last column. Text must be passed in as str type.
240-
text = np.asarray(text, dtype=str)
240+
text = np.asarray(text, dtype=np.str_)
241241
encoding = _check_encoding("".join(text.flatten()))
242242
if encoding != "ascii":
243243
text = np.vectorize(non_ascii_to_octal, excluded="encoding")(

pygmt/tests/test_clib_dataarray_to_matrix.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_dataarray_to_matrix_dims_fails():
7676
Check that it fails for > 2 dims.
7777
"""
7878
# Make a 3-D regular grid
79-
data = np.ones((10, 12, 11), dtype="float32")
79+
data = np.ones((10, 12, 11), dtype=np.float32)
8080
x = np.arange(11)
8181
y = np.arange(12)
8282
z = np.arange(10)
@@ -90,7 +90,7 @@ def test_dataarray_to_matrix_irregular_inc_warning():
9090
Check that it warns for variable increments, see also
9191
https://github.com/GenericMappingTools/pygmt/issues/1468.
9292
"""
93-
data = np.ones((4, 5), dtype="float64")
93+
data = np.ones((4, 5), dtype=np.float64)
9494
x = np.linspace(0, 1, 5)
9595
y = np.logspace(2, 3, 4)
9696
grid = xr.DataArray(data, coords=[("y", y), ("x", x)])
@@ -103,7 +103,7 @@ def test_dataarray_to_matrix_zero_inc_fails():
103103
"""
104104
Check that dataarray_to_matrix fails for zero increments grid.
105105
"""
106-
data = np.ones((5, 5), dtype="float32")
106+
data = np.ones((5, 5), dtype=np.float32)
107107
x = np.linspace(0, 1, 5)
108108
y = np.zeros_like(x)
109109
grid = xr.DataArray(data, coords=[("y", y), ("x", x)])

pygmt/tests/test_clib_put_strings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_put_strings():
2424
)
2525
x = np.array([1, 2, 3, 4, 5], dtype=np.int32)
2626
y = np.array([6, 7, 8, 9, 10], dtype=np.int32)
27-
strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=str)
27+
strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=np.str_)
2828
lib.put_vector(dataset, column=lib["GMT_X"], vector=x)
2929
lib.put_vector(dataset, column=lib["GMT_Y"], vector=y)
3030
lib.put_strings(
@@ -60,5 +60,5 @@ def test_put_strings_fails():
6060
lib.put_strings(
6161
dataset=None,
6262
family="GMT_IS_VECTOR|GMT_IS_DUPLICATE",
63-
strings=np.empty(shape=(3,), dtype=str),
63+
strings=np.empty(shape=(3,), dtype=np.str_),
6464
)

pygmt/tests/test_clib_put_vector.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ def test_put_vector_invalid_dtype():
208208
for dtype in [
209209
np.bool_,
210210
np.bytes_,
211-
np.csingle,
212-
np.cdouble,
213-
np.clongdouble,
214-
np.half,
211+
np.float16,
215212
np.longdouble,
213+
np.complex64,
214+
np.complex128,
215+
np.clongdouble,
216216
np.object_,
217217
]:
218218
with clib.Session() as lib:
@@ -224,7 +224,7 @@ def test_put_vector_invalid_dtype():
224224
)
225225
data = np.array([37, 12, 556], dtype=dtype)
226226
with pytest.raises(GMTInvalidInput, match="Unsupported numpy data type"):
227-
lib.put_vector(dataset, column=1, vector=data)
227+
lib.put_vector(dataset, column=0, vector=data)
228228

229229

230230
def test_put_vector_wrong_column():
@@ -238,7 +238,7 @@ def test_put_vector_wrong_column():
238238
mode="GMT_CONTAINER_ONLY",
239239
dim=[1, 3, 0, 0], # ncolumns, nrows, dtype, unused
240240
)
241-
data = np.array([37, 12, 556], dtype="float32")
241+
data = np.array([37, 12, 556], dtype=np.float32)
242242
with pytest.raises(GMTCLibError):
243243
lib.put_vector(dataset, column=1, vector=data)
244244

@@ -254,6 +254,6 @@ def test_put_vector_2d_fails():
254254
mode="GMT_CONTAINER_ONLY",
255255
dim=[1, 6, 0, 0], # ncolumns, nrows, dtype, unused
256256
)
257-
data = np.array([[37, 12, 556], [37, 12, 556]], dtype="int32")
257+
data = np.array([[37, 12, 556], [37, 12, 556]], dtype=np.int32)
258258
with pytest.raises(GMTInvalidInput):
259259
lib.put_vector(dataset, column=0, vector=data)

0 commit comments

Comments
 (0)