Skip to content

Commit

Permalink
Handle backwards compatibility for pandas<2
Browse files Browse the repository at this point in the history
Only use the date_format="ISO8601" argument when pandas>=2.0.0 is used. Set CI Tests Legacy to run with pandas<2 to ensure this works.
  • Loading branch information
weiji14 committed Jun 9, 2023
1 parent 615a585 commit fa2a6d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci_tests_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name: GMT Legacy Tests
on:
# push:
# branches: [ main ]
# pull_request:
pull_request:
# types: [ready_for_review]
# paths-ignore:
# - 'doc/**'
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
python=3.9
gmt=${{ matrix.gmt_version }}
numpy
pandas
pandas<2
xarray
netCDF4
packaging
Expand Down
8 changes: 7 additions & 1 deletion pygmt/src/x2sys_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -229,13 +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="ISO8601",
*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:]})
Expand Down

0 comments on commit fa2a6d3

Please sign in to comment.