Skip to content

[FEATURE]: allow "now" as input to cftime_range #6143

Open
@raybellwaves

Description

@raybellwaves

Is your feature request related to a problem?

For better or worse, I use xr.cftime_range interchangably with pd.date_range, mostly it is saves my importing pandas.

Pandas allows a string named "now" to be parsed in pd.date_range

pd.date_range("2000-01-01", "now", freq="H")

It would be nice if xr.cftime_range could do the same (see additional context for Traceback).

Describe the solution you'd like

Something like
assert pd.date_range("2000-01-01", "now", freq="H") == xr.cftime_range("2000-01-01", "now", freq="H")

Describe alternatives you've considered

Close as not within the scope of xr.cftime_range

Additional context

xr.cftime_range("2000-01-01", "now", freq="H")

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_15451/3415490845.py in <module>
----> 1 xr.cftime_range("2000-01-01", "now", freq="H")

/opt/userenvs/ray.bell/main/lib/python3.9/site-packages/xarray/coding/cftime_offsets.py in cftime_range(start, end, periods, freq, normalize, name, closed, calendar)
    982         start = _maybe_normalize_date(start, normalize)
    983     if end is not None:
--> 984         end = to_cftime_datetime(end, calendar)
    985         end = _maybe_normalize_date(end, normalize)
    986 

/opt/userenvs/ray.bell/main/lib/python3.9/site-packages/xarray/coding/cftime_offsets.py in to_cftime_datetime(date_str_or_date, calendar)
    697                 "a calendar type must be provided"
    698             )
--> 699         date, _ = _parse_iso8601_with_reso(get_date_type(calendar), date_str_or_date)
    700         return date
    701     elif isinstance(date_str_or_date, cftime.datetime):

/opt/userenvs/ray.bell/main/lib/python3.9/site-packages/xarray/coding/cftimeindex.py in _parse_iso8601_with_reso(date_type, timestr)
    125 
    126     default = date_type(1, 1, 1)
--> 127     result = parse_iso8601_like(timestr)
    128     replace = {}
    129 

/opt/userenvs/ray.bell/main/lib/python3.9/site-packages/xarray/coding/cftimeindex.py in parse_iso8601_like(datetime_string)
    115         if match:
    116             return match.groupdict()
--> 117     raise ValueError(
    118         f"no ISO-8601 or cftime-string-like match for string: {datetime_string}"
    119     )

ValueError: no ISO-8601 or cftime-string-like match for string: now

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions