Skip to content

Commit 6406b43

Browse files
committed
Allow pyarrow.TimestampScalar inputs to solar's terminator_datetime
Done by casting the pyarrow.TimestampScalar to a string first, before letting pd.to_datetime do the formatting. Have added a new parametrized unit test to test_solar_set_terminator_datetime with pyarrow and pd.Timestamp array_func to test this.
1 parent 850cc1d commit 6406b43

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

pygmt/src/solar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def solar(self, terminator="d", terminator_datetime=None, **kwargs):
111111
kwargs["T"] = terminator[0]
112112
if terminator_datetime:
113113
try:
114-
datetime_string = pd.to_datetime(terminator_datetime).strftime(
114+
datetime_string = pd.to_datetime(str(terminator_datetime)).strftime(
115115
"%Y-%m-%dT%H:%M:%S.%f"
116116
)
117117
except ValueError as verr:

pygmt/tests/test_solar.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
"""
44
import datetime
55

6+
import pandas as pd
67
import pytest
78
from pygmt import Figure
89
from pygmt.exceptions import GMTInvalidInput
10+
from pygmt.helpers.testing import skip_if_no
11+
12+
try:
13+
import pyarrow.compute as pc
14+
except ImportError:
15+
pc = None
916

1017

1118
@pytest.mark.mpl_image_compare
@@ -41,17 +48,28 @@ def test_solar_terminators():
4148

4249
@pytest.mark.mpl_image_compare(filename="test_solar_set_terminator_datetime.png")
4350
@pytest.mark.parametrize(
44-
"terminator_datetime",
51+
"array_func",
4552
[
46-
pytest.param("1990-02-17 04:25:00", id="terminator_datetime_string"),
47-
datetime.datetime(year=1990, month=2, day=17, hour=4, minute=25, second=0),
53+
str,
54+
datetime.datetime.fromisoformat,
55+
pd.Timestamp,
56+
pytest.param(
57+
getattr(pc, "strptime", None), marks=skip_if_no(package="pyarrow")
58+
),
4859
],
4960
)
50-
def test_solar_set_terminator_datetime(terminator_datetime):
61+
def test_solar_set_terminator_datetime(array_func):
5162
"""
5263
Test passing the solar argument with the day_night terminator and a
5364
datetime string.
5465
"""
66+
kwargs = (
67+
{"format": "%Y-%m-%d %H:%M:%S", "unit": "s"}
68+
if array_func.__name__ == "strptime"
69+
else {}
70+
)
71+
terminator_datetime = array_func("1990-02-17 04:25:00", **kwargs)
72+
5573
fig = Figure()
5674
fig.solar(
5775
region="d",

0 commit comments

Comments
 (0)