Skip to content

Commit

Permalink
Convert dummpy times to timedelta
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Apr 22, 2024
1 parent d6eeade commit 9d12ae1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions pygmt/src/x2sys_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,14 @@ def x2sys_cross(
vfname=vouttbl, output_type=output_type, header=2
)

# Convert 3rd and 4th columns to datetimes.
# Convert 3rd and 4th columns to datetime or timedelta.
# These two columns have names "t_1"/"t_2" or "i_1"/"i_2".
# "t_1"/"t_2" means they are datetimes and should be converted.
# "i_1"/"i_2" means they are dummy times (i.e., floating-point values).
if output_type == "pandas" and result.columns[2] == "t_1":
# "t_1"/"t_2" means they are absolute datetimes.
# "i_1"/"i_2" means they are dummy times relative to unix epoch.
if output_type == "pandas":
t_or_i = result.columns[2][0]
to_func = {"t": pd.to_datetime, "i": pd.to_timedelta}[t_or_i]
result[result.columns[2:4]] = result[result.columns[2:4]].apply(
pd.to_datetime, unit="s"
to_func, unit="s"
)
return result
8 changes: 4 additions & 4 deletions pygmt/tests/test_x2sys_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def test_x2sys_cross_input_dataframe_output_dataframe(tracks):
columns = list(output.columns)
assert columns[:6] == ["x", "y", "i_1", "i_2", "dist_1", "dist_2"]
assert columns[6:] == ["head_1", "head_2", "vel_1", "vel_2", "z_X", "z_M"]
assert output.dtypes["i_1"].type == np.float64
assert output.dtypes["i_2"].type == np.float64
assert output.dtypes["i_1"].type == np.timedelta64
assert output.dtypes["i_2"].type == np.timedelta64


@pytest.mark.usefixtures("mock_x2sys_home")
Expand Down Expand Up @@ -162,8 +162,8 @@ def test_x2sys_cross_input_dataframe_with_nan(tracks):
columns = list(output.columns)
assert columns[:6] == ["x", "y", "i_1", "i_2", "dist_1", "dist_2"]
assert columns[6:] == ["head_1", "head_2", "vel_1", "vel_2", "z_X", "z_M"]
assert output.dtypes["i_1"].type == np.float64
assert output.dtypes["i_2"].type == np.float64
assert output.dtypes["i_1"].type == np.timedelta64
assert output.dtypes["i_2"].type == np.timedelta64


@pytest.mark.usefixtures("mock_x2sys_home")
Expand Down

0 comments on commit 9d12ae1

Please sign in to comment.