Skip to content

Adapt exception handling logic in CFTimeIndex.__sub__ and __rsub__ - #5006

Merged
spencerkclark merged 1 commit into
pydata:masterfrom
spencerkclark:fix-cftimeindex-sub-error
Mar 7, 2021
Merged

Adapt exception handling logic in CFTimeIndex.__sub__ and __rsub__#5006
spencerkclark merged 1 commit into
pydata:masterfrom
spencerkclark:fix-cftimeindex-sub-error

Conversation

@spencerkclark

@spencerkclarkspencerkclark commented Mar 7, 2021

Copy link
Copy Markdown
Member

The exception that was raised in pandas when a datetime.timedelta object outside the range that could be expressed in units of nanoseconds was passed to the pandas.TimedeltaIndex constructor changed from an OverflowError to an OutOfBoundsTimedelta error in the development version of pandas. This PR adjusts our exception handling logic in CFTimeIndex.__sub__ and CFTimeIndex.__rsub__ to account for this.

Details

Previous versions of pandas:

>>>importpandasaspd; fromdatetimeimporttimedelta>>>pd.TimedeltaIndex([timedelta(days=300*365)])
Traceback (mostrecentcalllast):
File"pandas/_libs/tslibs/timedeltas.pyx", line263, inpandas._libs.tslibs.timedeltas.array_to_timedelta64TypeError: Expectedunicode, gotdatetime.timedeltaDuringhandlingoftheaboveexception, anotherexceptionoccurred:
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"/Users/spencer/Software/miniconda3/envs/xarray-tests/lib/python3.7/site-packages/pandas/core/indexes/timedeltas.py", line157, in__new__data, freq=freq, unit=unit, dtype=dtype, copy=copyFile"/Users/spencer/Software/miniconda3/envs/xarray-tests/lib/python3.7/site-packages/pandas/core/arrays/timedeltas.py", line216, in_from_sequencedata, inferred_freq=sequence_to_td64ns(data, copy=copy, unit=unit)
File"/Users/spencer/Software/miniconda3/envs/xarray-tests/lib/python3.7/site-packages/pandas/core/arrays/timedeltas.py", line926, insequence_to_td64nsdata=objects_to_td64ns(data, unit=unit, errors=errors)
File"/Users/spencer/Software/miniconda3/envs/xarray-tests/lib/python3.7/site-packages/pandas/core/arrays/timedeltas.py", line1036, inobjects_to_td64nsresult=array_to_timedelta64(values, unit=unit, errors=errors)
File"pandas/_libs/tslibs/timedeltas.pyx", line268, inpandas._libs.tslibs.timedeltas.array_to_timedelta64File"pandas/_libs/tslibs/timedeltas.pyx", line221, inpandas._libs.tslibs.timedeltas.convert_to_timedelta64File"pandas/_libs/tslibs/timedeltas.pyx", line166, inpandas._libs.tslibs.timedeltas.delta_to_nanosecondsOverflowError: PythoninttoolargetoconverttoClong

Development version of pandas:

>>>importpandasaspd; fromdatetimeimporttimedelta>>>pd.TimedeltaIndex([timedelta(days=300*365)])
Traceback (mostrecentcalllast):
File"pandas/_libs/tslibs/timedeltas.pyx", line348, inpandas._libs.tslibs.timedeltas.array_to_timedelta64TypeError: Expectedunicode, gotdatetime.timedeltaDuringhandlingoftheaboveexception, anotherexceptionoccurred:
Traceback (mostrecentcalllast):
File"pandas/_libs/tslibs/timedeltas.pyx", line186, inpandas._libs.tslibs.timedeltas.delta_to_nanosecondsOverflowError: PythoninttoolargetoconverttoClongTheaboveexceptionwasthedirectcauseofthefollowingexception:
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"/Users/spencer/software/pandas/pandas/core/indexes/timedeltas.py", line161, in__new__tdarr=TimedeltaArray._from_sequence_not_strict(
File"/Users/spencer/software/pandas/pandas/core/arrays/timedeltas.py", line270, in_from_sequence_not_strictdata, inferred_freq=sequence_to_td64ns(data, copy=copy, unit=unit)
File"/Users/spencer/software/pandas/pandas/core/arrays/timedeltas.py", line970, insequence_to_td64nsdata=objects_to_td64ns(data, unit=unit, errors=errors)
File"/Users/spencer/software/pandas/pandas/core/arrays/timedeltas.py", line1079, inobjects_to_td64nsresult=array_to_timedelta64(values, unit=unit, errors=errors)
File"pandas/_libs/tslibs/timedeltas.pyx", line362, inpandas._libs.tslibs.timedeltas.array_to_timedelta64File"pandas/_libs/tslibs/timedeltas.pyx", line353, inpandas._libs.tslibs.timedeltas.array_to_timedelta64File"pandas/_libs/tslibs/timedeltas.pyx", line306, inpandas._libs.tslibs.timedeltas.convert_to_timedelta64File"pandas/_libs/tslibs/timedeltas.pyx", line189, inpandas._libs.tslibs.timedeltas.delta_to_nanosecondspandas._libs.tslibs.conversion.OutOfBoundsTimedelta: PythoninttoolargetoconverttoClong

… and __rsub__
The latest version of pandas raises an OutOfBoundsTimedelta error instead
of an Overflow error.
@spencerkclark
spencerkclarkforce-pushed the fix-cftimeindex-sub-error branch from ba60d9b to a62bd7bCompareMarch 7, 2021 12:45
@spencerkclark
spencerkclark merged commit b610a3c into pydata:masterMar 7, 2021
@spencerkclark
spencerkclark deleted the fix-cftimeindex-sub-error branch March 7, 2021 13:22
dcherian added a commit to dcherian/xarray that referenced this pull request Mar 8, 2021
* upstream/master: (46 commits)
pin netCDF4=1.5.3 in min-all-deps (pydata#4982)
fix matplotlib errors for single level discrete colormaps (pydata#4256)
Adapt exception handling in CFTimeIndex.__sub__ and __rsub__ (pydata#5006)
Update options.py (pydata#5000)
Adjust tests to use updated pandas syntax for offsets (pydata#4537)
add a combine_attrs parameter to Dataset.merge (pydata#4895)
Support for dask.graph_manipulation (pydata#4965)
raise on passing axis to Dataset.reduce methods (pydata#4940)
Whatsnew for 0.17.1 (pydata#4963)
Refinements to how-to-release (pydata#4964)
DOC: add example for reindex (pydata#4956)
DOC: rm np import (pydata#4949)
Add 0.17.0 release notes (pydata#4953)
document update as inplace (pydata#4932)
bump the dependencies (pydata#4942)
Upstream CI: limit runtime (pydata#4946)
typing for numpy 1.20 (pydata#4878)
Use definition of DTypeLike from Numpy if found (pydata#4941)
autoupdate mypy (pydata#4943)
Add DataArrayCoarsen.reduce and DatasetCoarsen.reduce methods (pydata#4939)
...
dcherian added a commit to dcherian/xarray that referenced this pull request Mar 18, 2021
…indow
* upstream/master:
add polyval to polyfit see also (pydata#5020)
mention map_blocks in the docstring of apply_ufunc (pydata#5011)
Switch backend API to v2 (pydata#4989)
WIP: add new backend api documentation (pydata#4810)
pin netCDF4=1.5.3 in min-all-deps (pydata#4982)
fix matplotlib errors for single level discrete colormaps (pydata#4256)
Adapt exception handling in CFTimeIndex.__sub__ and __rsub__ (pydata#5006)
Update options.py (pydata#5000)
Adjust tests to use updated pandas syntax for offsets (pydata#4537)
add a combine_attrs parameter to Dataset.merge (pydata#4895)
Support for dask.graph_manipulation (pydata#4965)
raise on passing axis to Dataset.reduce methods (pydata#4940)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

⚠️ Nightly upstream-dev CI failed ⚠️

3 participants

@spencerkclark@andersy005@keewis