pull_requests: 657205536
This data as json
id | node_id | number | state | locked | title | user | body | created_at | updated_at | closed_at | merged_at | merge_commit_sha | assignee | milestone | draft | head | base | author_association | auto_merge | repo | url | merged_by |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
657205536 | MDExOlB1bGxSZXF1ZXN0NjU3MjA1NTM2 | 5402 | open | 0 | `dt.to_pytimedelta` to allow arithmetic with cftime objects | 20629530 | <!-- Feel free to remove check-list items aren't relevant to your change --> - [ ] Closes #xxxx - [x] Tests added - [x] Passes `pre-commit run --all-files` - [ ] User visible changes (including notable bug fixes) are documented in `whats-new.rst` - [ ] New functions/methods are listed in `api.rst` When playing with cftime objects a problem I encountered many times is that I can sub two arrays and them add it back to another. Subtracting to cftime datetime arrays result in an array of `np.timedelta64`. And when trying to add it back to another cftime array, we get a `UFuncTypeError` because the two arrays have incompatible dtypes : '<m8[ns]' and 'O'. Example: ```python import xarray as xr da = xr.DataArray(xr.cftime_range('1900-01-01', freq='D', periods=10), dims=('time',)) # An array of timedelta64[ns] dt = da - da[0] da[-1] + dt # Fails ``` However, if the two arrays were of 'O' dtype, then the subtraction would be made by `cftime` which supports `datetime.timedelta` objects. This solution here adds a `to_pytimedelta` to the `TimedeltaAccessor`, mirroring the name of the similar function on `pd.Series.dt`. It uses a monkeypatching workaround to prevent xarray to case the array back into numpy objects. The user still has to check if the data is in cftime or numpy to adapt the operation (calling `dt.to_pytimedelta` or not), but custom workaround were always overly complicated for such a simple problem, this helps. Also, this doesn't work with dask arrays because loading a dask array triggers the variable constructor and thus recasts the array of `datetime.timedelta` to `numpy.timedelta[64]`. I realize I maybe should have opened an issue before, but I had this idea and it all rushed along. | 2021-05-28T22:48:50Z | 2022-06-09T14:50:16Z | 0060277e4ecf1b05a198aeff9051d86f814b0096 | 0 | 71d567789573b47e059dbaebabcbda9c3493d0c5 | d1e4164f3961d7bbb3eb79037e96cae14f7182f8 | CONTRIBUTOR | 13221727 | https://github.com/pydata/xarray/pull/5402 |
Links from other tables
- 1 row from pull_requests_id in labels_pull_requests