home / github / issues

Menu
  • GraphQL API
  • Search all tables

issues: 677307460

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
677307460 MDU6SXNzdWU2NzczMDc0NjA= 4337 cftime_range does not support default cftime.datetime formatted output strings 6063709 closed 0     5 2020-08-12T01:28:30Z 2020-08-17T23:27:07Z 2020-08-17T23:27:07Z CONTRIBUTOR      

Is your feature request related to a problem? Please describe.

The xarray.cftime_range does not support datetime strings that are the default output from cftime.datetime.strftime() which are the format which cftime_range itself uses internally.

python import cftime import xarray date = cftime.datetime(10,1,1).strftime() print(date) xarray.cftime_range(date, periods=3, freq='Y') outputs ``` 10-01-01 00:00:00


ValueError Traceback (most recent call last) <ipython-input-70-a16c1fcab8d6> in <module> 3 date = cftime.datetime(10,1,1).strftime() 4 print(date) ----> 5 xarray.cftime_range(date, periods=3, freq='Y')

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-20.07/lib/python3.7/site-packages/xarray/coding/cftime_offsets.py in cftime_range(start, end, periods, freq, normalize, name, closed, calendar) 963 964 if start is not None: --> 965 start = to_cftime_datetime(start, calendar) 966 start = _maybe_normalize_date(start, normalize) 967 if end is not None:

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-20.07/lib/python3.7/site-packages/xarray/coding/cftime_offsets.py in to_cftime_datetime(date_str_or_date, calendar) 683 "a calendar type must be provided" 684 ) --> 685 date, _ = _parse_iso8601_with_reso(get_date_type(calendar), date_str_or_date) 686 return date 687 elif isinstance(date_str_or_date, cftime.datetime):

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-20.07/lib/python3.7/site-packages/xarray/coding/cftimeindex.py in _parse_iso8601_with_reso(date_type, timestr) 101 102 default = date_type(1, 1, 1) --> 103 result = parse_iso8601(timestr) 104 replace = {} 105

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-20.07/lib/python3.7/site-packages/xarray/coding/cftimeindex.py in parse_iso8601(datetime_string) 94 if match: 95 return match.groupdict() ---> 96 raise ValueError("no ISO-8601 match for string: %s" % datetime_string) 97 98

ValueError: no ISO-8601 match for string: 10-01-01 00:00:00 ```

Describe the solution you'd like It would be good if xarray.cftime_range supported the default strftime format output from cftime.datetime objects. It is confusing that it uses this format with repr but explicitly does not support it.

Describe alternatives you've considered

Specifying an ISO-8601 compatible format (using T separator) isn't general as it doesn't work for years < 1000 because the year field is not zero padded. python import cftime import xarray date = cftime.datetime(10,1,1).strftime('%Y-%m-%dT%H:%M:%S') print('|{}|'.format(date)) xarray.cftime_range(date, periods=3, freq='Y') produces | 10-01-01T00:00:00| and the error as above.

A work-around is to zero-pad manually python import cftime import xarray date = '{:0>19}'.format(cftime.datetime(10,1,1).strftime('%Y-%m-%dT%H:%M:%S').lstrip()) print(date) xarray.cftime_range(date, periods=3, freq='Y') produces 0010-01-01T00:00:00 CFTimeIndex([0010-12-31 00:00:00, 0011-12-31 00:00:00, 0012-12-31 00:00:00], dtype='object')

Additional context I think this is a relatively small addition to the codebase but would make it easier and less confusing to use the default format that is also used by the the function itself. It is easy to support as it is consistent and uniform.

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/4337/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 1 row from issues_id in issues_labels
  • 5 rows from issue in issue_comments
Powered by Datasette · Queries took 2.696ms · About: xarray-datasette