Skip to content

Commit

Permalink
Allow pyarrow.TimestampScalar inputs to solar's terminator_datetime
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weiji14 committed Dec 17, 2023
1 parent 850cc1d commit 6406b43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pygmt/src/solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def solar(self, terminator="d", terminator_datetime=None, **kwargs):
kwargs["T"] = terminator[0]
if terminator_datetime:
try:
datetime_string = pd.to_datetime(terminator_datetime).strftime(
datetime_string = pd.to_datetime(str(terminator_datetime)).strftime(
"%Y-%m-%dT%H:%M:%S.%f"
)
except ValueError as verr:
Expand Down
26 changes: 22 additions & 4 deletions pygmt/tests/test_solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
"""
import datetime

import pandas as pd
import pytest
from pygmt import Figure
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers.testing import skip_if_no

try:
import pyarrow.compute as pc
except ImportError:
pc = None


@pytest.mark.mpl_image_compare
Expand Down Expand Up @@ -41,17 +48,28 @@ def test_solar_terminators():

@pytest.mark.mpl_image_compare(filename="test_solar_set_terminator_datetime.png")
@pytest.mark.parametrize(
"terminator_datetime",
"array_func",
[
pytest.param("1990-02-17 04:25:00", id="terminator_datetime_string"),
datetime.datetime(year=1990, month=2, day=17, hour=4, minute=25, second=0),
str,
datetime.datetime.fromisoformat,
pd.Timestamp,
pytest.param(
getattr(pc, "strptime", None), marks=skip_if_no(package="pyarrow")
),
],
)
def test_solar_set_terminator_datetime(terminator_datetime):
def test_solar_set_terminator_datetime(array_func):
"""
Test passing the solar argument with the day_night terminator and a
datetime string.
"""
kwargs = (
{"format": "%Y-%m-%d %H:%M:%S", "unit": "s"}
if array_func.__name__ == "strptime"
else {}
)
terminator_datetime = array_func("1990-02-17 04:25:00", **kwargs)

fig = Figure()
fig.solar(
region="d",
Expand Down

0 comments on commit 6406b43

Please sign in to comment.