pydata / pydata/xarray

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

Open
#6,143 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement topic-cftime
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at xarray/coding/cftime_offsets.py, where cftime_range passes its end value to to_cftime_datetime, and inspect the traceback path through xarray/coding/cftimeindex.py. Compare the requested behavior with pandas date_range for the shown inputs; done means cftime_range accepts "now" with equivalent range semantics and coverage verifies it.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.