Skip to content

Conversation

@weiji14
Copy link
Member

@weiji14 weiji14 commented Aug 21, 2025

Description of proposed changes

Fixes IndexError: index 0 is out of bounds for axis 0 with size 0 when x2sys_cross returns 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: IndexError

Fixes 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

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash command is:

  • /format: automatically format and lint the code

Fixes `IndexError: index 0 is out of bounds for axis 0 with size 0` when x2sys_cross returns no crossovers. An empty dataframe should be returned instead.
@weiji14 weiji14 added this to the 0.17.0 milestone Aug 21, 2025
@weiji14 weiji14 self-assigned this Aug 21, 2025
@weiji14 weiji14 added the bug Something isn't working label Aug 21, 2025
Avoid checking of column names when the dataframe is empty due to no available crossovers.
@weiji14 weiji14 changed the title Add regression test to ensure x2sys_cross can output empty dataframe x2sys_cross: Fix IndexError to allow empty dataframe outputs Aug 21, 2025
@weiji14 weiji14 marked this pull request as ready for review August 21, 2025 01:59
@weiji14 weiji14 enabled auto-merge (squash) August 21, 2025 04:47
@weiji14 weiji14 merged commit 4c6a1f8 into main Aug 21, 2025
23 of 24 checks passed
@weiji14 weiji14 deleted the x2sys_empty_df branch August 21, 2025 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants