Repository navigation
Fix sequence_join treating any string with a space as a datetime - #4961
Conversation
|
@Mohit-Ak Are you interested in implementing the solution in #4960 (comment)? |
04b83f0 to
212aea5
Compare
|
@seisman thanks, done: switched to your try/except approach, so pandas timestamps and datetime strings with spaces still go through |
| except ValueError: # Not a valid datetime. Assume it's a plain string | ||
| _values.append(str(item)) | ||
| else: | ||
| _values.append(str(item)) |
There was a problem hiding this comment.
Actually, can these two branches be collapsed somehow?
There was a problem hiding this comment.
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)There was a problem hiding this comment.
Done, switched to your version; helper tests and doctests still pass.
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
|
/format |
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Description of proposed changes
sequence_joindecided whether an item was datetime-like by checking for a space instr(item), so any plain string containing a space was fed tonp.asarray(item, dtype="datetime64")and raisedValueError: Cannot create a NumPy datetime other than NaT with generic units. EveryAlias(..., sep=...)goes through this, somagnetic_rose/directional_rosecrashed on free-text labels likelabels=["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 isdatetime64, which coversnp.datetime64, 0-d arrays andxr.DataArray). Plain strings are passed through unchanged. GMT itself accepts a space inside+llabels 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 onmain, passes here), thesequence_join/aliasdoctests,test_helpers,test_alias_system, and the magnetic/directional rose image tests, plusruff check,ruff format --checkandmypyon the changed files.Fixes #4960