Skip to content

Set date_format to ISO8601 to silence pandas 2.0 UserWarning on read_csv #2569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci_tests_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
python=3.9
gmt=${{ matrix.gmt_version }}
numpy
pandas
pandas<2
xarray
netCDF4
packaging
Expand Down
9 changes: 8 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 @@ -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:
Expand Down Expand Up @@ -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:]})
Expand Down
2 changes: 1 addition & 1 deletion pygmt/tests/test_x2sys_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down