diff --git a/.github/workflows/ci_tests_legacy.yaml b/.github/workflows/ci_tests_legacy.yaml index 28200dd2cb9..d484867d6bb 100644 --- a/.github/workflows/ci_tests_legacy.yaml +++ b/.github/workflows/ci_tests_legacy.yaml @@ -67,7 +67,7 @@ jobs: python=3.9 gmt=${{ matrix.gmt_version }} numpy - pandas + pandas<2 xarray netCDF4 packaging diff --git a/pygmt/src/x2sys_cross.py b/pygmt/src/x2sys_cross.py index beb02bc7e5c..9ecce31d6ad 100644 --- a/pygmt/src/x2sys_cross.py +++ b/pygmt/src/x2sys_cross.py @@ -6,6 +6,7 @@ from pathlib import Path import pandas as pd +from packaging.version import Version from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( @@ -47,7 +48,7 @@ def tempfile_from_dftrack(track, suffix): sep="\t", index=False, na_rep="NaN", # write a NaN value explicitly instead of a blank string - date_format="%Y-%m-%dT%H:%M:%S.%fZ", + date_format="%Y-%m-%dT%H:%M:%S.%fZ", # ISO8601 format ) yield tmpfilename finally: @@ -229,12 +230,18 @@ def x2sys_cross(tracks=None, outfile=None, **kwargs): # Read temporary csv output to a pandas table if outfile == tmpfile.name: # if outfile isn't set, return pd.DataFrame # Read the tab-separated ASCII table + date_format_kwarg = ( + {"date_format": "ISO8601"} + if Version(pd.__version__) >= Version("2.0.0") + else {} + ) table = pd.read_csv( tmpfile.name, sep="\t", header=2, # Column names are on 2nd row comment=">", # Skip the 3rd row with a ">" parse_dates=[2, 3], # Datetimes on 3rd and 4th column + **date_format_kwarg, # Parse dates in ISO8601 format on pandas>=2 ) # Remove the "# " from "# x" in the first column table = table.rename(columns={table.columns[0]: table.columns[0][2:]}) diff --git a/pygmt/tests/test_x2sys_cross.py b/pygmt/tests/test_x2sys_cross.py index 34fe668b8cf..f592174ab77 100644 --- a/pygmt/tests/test_x2sys_cross.py +++ b/pygmt/tests/test_x2sys_cross.py @@ -112,7 +112,7 @@ def test_x2sys_cross_input_two_dataframes(mock_x2sys_home): for i in range(2): np.random.seed(seed=i) track = pd.DataFrame(data=np.random.rand(10, 3), columns=("x", "y", "z")) - track["time"] = pd.date_range(start=f"2020-{i}1-01", periods=10, freq="ms") + track["time"] = pd.date_range(start=f"2020-{i}1-01", periods=10, freq="min") tracks.append(track) output = x2sys_cross(tracks=tracks, tag=tag, coe="e")