Skip to content

Commit

Permalink
Fix bug so that x2sys_cross accepts dataframes with NaN values (#1369)
Browse files Browse the repository at this point in the history
Ensure that the NaN values properly written to the temporary
track file in tempfile_from_dftrack so that GMT's `x2sys_cross`
function can read it properly. Added a regression unit test to
ensure that the pandas EmptyDataError doesn't occur again.
  • Loading branch information
weiji14 authored Jul 9, 2021
1 parent 11c1301 commit 60a92b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions pygmt/src/x2sys_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def tempfile_from_dftrack(track, suffix):
path_or_buf=tmpfilename,
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",
)
yield tmpfilename
Expand Down
25 changes: 24 additions & 1 deletion pygmt/tests/test_x2sys_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_x2sys_cross_input_file_output_dataframe(mock_x2sys_home):

def test_x2sys_cross_input_dataframe_output_dataframe(mock_x2sys_home, tracks):
"""
Run x2sys_cross by passing in one dataframe, and output external crossovers
Run x2sys_cross by passing in one dataframe, and output internal crossovers
to a pandas.DataFrame.
"""
with TemporaryDirectory(prefix="X2SYS", dir=os.getcwd()) as tmpdir:
Expand Down Expand Up @@ -129,6 +129,29 @@ def test_x2sys_cross_input_two_dataframes(mock_x2sys_home):
assert output.dtypes["t_2"].type == np.datetime64


def test_x2sys_cross_input_dataframe_with_nan(mock_x2sys_home, tracks):
"""
Run x2sys_cross by passing in one dataframe with NaN values, and output
internal crossovers to a pandas.DataFrame.
"""
with TemporaryDirectory(prefix="X2SYS", dir=os.getcwd()) as tmpdir:
tag = os.path.basename(tmpdir)
x2sys_init(
tag=tag, fmtfile="xyz", suffix="xyzt", units=["de", "se"], force=True
)

tracks[0].loc[tracks[0]["z"] < -15, "z"] = np.nan # set some values to NaN
output = x2sys_cross(tracks=tracks, tag=tag, coe="i")

assert isinstance(output, pd.DataFrame)
assert output.shape == (3, 12)
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.object_
assert output.dtypes["i_2"].type == np.object_


def test_x2sys_cross_input_two_filenames(mock_x2sys_home):
"""
Run x2sys_cross by passing in two filenames, and output external crossovers
Expand Down

0 comments on commit 60a92b3

Please sign in to comment.