x2sys_cross: Fix IndexError to allow empty dataframe outputs #4060
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of proposed changes
Fixes
IndexError: index 0 is out of bounds for axis 0 with size 0whenx2sys_crossreturns no crossovers. An empty dataframe should be returned instead.Full traceback:
___________________ test_x2sys_cross_output_dataframe_empty ____________________ self = RangeIndex(start=0, stop=0, step=1), key = 0 def __getitem__(self, key): """ Conserve RangeIndex type for scalar and slice keys. """ if isinstance(key, slice): return self._getitem_slice(key) elif is_integer(key): new_key = int(key) try: > return self._range[new_key] ^^^^^^^^^^^^^^^^^^^^ E IndexError: range object index out of range ../../../../micromamba/envs/pygmt/lib/python3.11/site-packages/pandas/core/indexes/range.py:982: IndexError The above exception was the direct cause of the following exception: tracks = [ x y z 4152 251.11450 25.22122 -19.0 4380 249.61510 24.30340 -19.0 5033 249.36575 27.34900 -19.0 5034 249.36971 27.34747 -19.0 9589 249.72477 24.39497 -13.0] @pytest.mark.benchmark @pytest.mark.usefixtures("mock_x2sys_home") def test_x2sys_cross_output_dataframe_empty(tracks): """ Test that x2sys_cross can output an empty dataframe (when there are no crossovers) without any errors. Regression test for https://forum.generic-mapping-tools.org/t/issue-with-x2sys-in-pygmt-solved/6154 """ with TemporaryDirectory(prefix="X2SYS", dir=Path.cwd()) as tmpdir: tag = Path(tmpdir).name x2sys_init(tag=tag, fmtfile="xyz", force=True) tracks = [tracks[0][:5]] # subset to less rows so there won't be crossovers > output = x2sys_cross(tracks=tracks, tag=tag, coe="i") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ../pygmt/tests/test_x2sys_cross.py:328: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../pygmt/helpers/decorators.py:590: in new_module return module_func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ../pygmt/helpers/decorators.py:756: in new_module return module_func(*bound.args, **bound.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ../pygmt/src/x2sys_cross.py:256: in x2sys_cross if columns[0][0] == "t": # "t" or "i": ^^^^^^^^^^ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = RangeIndex(start=0, stop=0, step=1), key = 0 def __getitem__(self, key): """ Conserve RangeIndex type for scalar and slice keys. """ if isinstance(key, slice): return self._getitem_slice(key) elif is_integer(key): new_key = int(key) try: return self._range[new_key] except IndexError as err: > raise IndexError( f"index {key} is out of bounds for axis 0 with size {len(self)}" E IndexError: index 0 is out of bounds for axis 0 with size 0 ../../../../micromamba/envs/pygmt/lib/python3.11/site-packages/pandas/core/indexes/range.py:984: IndexErrorFixes https://forum.generic-mapping-tools.org/t/issue-with-x2sys-in-pygmt-solved/6154, patches bug introduced in #3182 (pygmt v0.13.0 - v0.16.0)
Reminders
make formatandmake checkto make sure the code follows the style guide.doc/api/index.rst.Slash Commands
You can write slash commands (
/command) in the first line of a comment to performspecific operations. Supported slash command is:
/format: automatically format and lint the code