Skip to content

Fix sequence_join treating any string with a space as a datetime - #4961

Merged
seisman merged 4 commits into
GenericMappingTools:mainfrom
Mohit-Ak:fix/sequence-join-string-labels
Nov 6, 2026
Merged

seisman merged 4 commits into
GenericMappingTools:mainfrom
Mohit-Ak:fix/sequence-join-string-labels

Conversation

@Mohit-Ak

Copy link
Copy Markdown
Contributor

Description of proposed changes

sequence_join decided whether an item was datetime-like by checking for a space in str(item), so any plain string containing a space was fed to np.asarray(item, dtype="datetime64") and raised ValueError: Cannot create a NumPy datetime other than NaT with generic units. Every Alias(..., sep=...) goes through this, so magnetic_rose/directional_rose crashed on free-text labels like labels=["W", "E", "S", "Pole Nord"] before GMT was called.

This replaces the space check with a type check (datetime.date/datetime.datetime/pd.Timestamp, or anything whose array dtype is datetime64, which covers np.datetime64, 0-d arrays and xr.DataArray). Plain strings are passed through unchanged. GMT itself accepts a space inside +l labels when the option is passed as a separate argv entry, so no escaping is needed.

Tested with the new test_sequence_join_strings_with_spaces (fails on main, passes here), the sequence_join/alias doctests, test_helpers, test_alias_system, and the magnetic/directional rose image tests, plus ruff check, ruff format --check and mypy on the changed files.

Fixes #4960

Comment thread pygmt/helpers/utils.py Resolved
@seisman

seisman commented Nov 2, 2026

Copy link
Copy Markdown
Member

@Mohit-Ak Are you interested in implementing the solution in #4960 (comment)?

@Mohit-Ak
Mohit-Ak force-pushed the fix/sequence-join-string-labels branch from 04b83f0 to 212aea5 Compare November 3, 2026 03:42
@Mohit-Ak

Mohit-Ak commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@seisman thanks, done: switched to your try/except approach, so pandas timestamps and datetime strings with spaces still go through np.asarray(..., dtype="datetime64"), and plain labels like "North Pole" fall back to str.
Added tests for the label, a datetime string with a space and a pd.Timestamp. A pyarrow timestamp scalar falls back to str (it used to raise); happy to add explicit handling if you want it here.

Comment thread pygmt/helpers/utils.py Outdated
Comment on lines 930 to 933
except ValueError: # Not a valid datetime. Assume it's a plain string
_values.append(str(item))
else:
_values.append(str(item))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, can these two branches be collapsed somehow?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I feel the codes like below is more readable:

        _values = []
        for item in value:
            _value = str(item)
            if isinstance(item, np.timedelta64):  # Convert timedelta64 to numeric value
                _value = str(item.astype(int))
            elif " " in _value:  # Likely a datetime-lik object.
                with contextlib.suppress(ValueError):
                    _value = np.datetime_as_string(np.asarray(item, dtype="datetime64"))  # type: ignore[arg-type]
            _values.append(_value)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, switched to your version; helper tests and doctests still pass.

Comment thread pygmt/tests/test_helpers.py Resolved
@seisman seisman added the bug Something isn't working label Nov 6, 2026
@seisman seisman added this to the 0.20.0 milestone Nov 6, 2026
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seisman

seisman commented Nov 6, 2026

Copy link
Copy Markdown
Member

/format

Comment thread pygmt/tests/test_helpers.py Resolved
Comment thread pygmt/helpers/utils.py Resolved
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@seisman
seisman merged commit 93c1750 into GenericMappingTools:main Nov 6, 2026
16 of 18 checks passed
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.

sequence_join treats any string containing a space as a datetime (crashes labels= in magnetic_rose/directional_rose)

3 participants