Skip to content

Commit f65b764

Browse files
committed
Address review comments
1 parent 1a6841d commit f65b764

File tree

7 files changed

+29
-20
lines changed

7 files changed

+29
-20
lines changed

xarray/coding/cftimeindex.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,15 @@ def _add_delta(self, deltas):
384384
# pandas. No longer used as of pandas 0.23.
385385
return self + deltas
386386

387-
def to_datetimeindex(self):
387+
def to_datetimeindex(self, unsafe=False):
388388
"""If possible, convert this index to a pandas.DatetimeIndex.
389389
390+
Parameters
391+
----------
392+
unsafe : bool
393+
Flag to turn off warning when converting from a CFTimeIndex with
394+
a non-standard calendar to a DatetimeIndex (default ``False``).
395+
390396
Returns
391397
-------
392398
pandas.DatetimeIndex
@@ -419,7 +425,7 @@ def to_datetimeindex(self):
419425
""" # noqa: E501
420426
nptimes = cftime_to_nptime(self)
421427
calendar = infer_calendar_name(self)
422-
if calendar not in _STANDARD_CALENDARS:
428+
if calendar not in _STANDARD_CALENDARS and not unsafe:
423429
warnings.warn(
424430
'Converting a CFTimeIndex with dates from a non-standard '
425431
'calendar, {!r}, to a pandas.DatetimeIndex, which uses dates '

xarray/coding/times.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ def cftime_to_nptime(times):
280280
new = np.empty(times.shape, dtype='M8[ns]')
281281
for i, t in np.ndenumerate(times):
282282
try:
283+
# Use pandas.Timestamp in place of datetime.datetime, because
284+
# NumPy casts it safely it np.datetime64[ns] for dates outside
285+
# 1678 to 2262 (this is not currently the case for
286+
# datetime.datetime).
283287
dt = pd.Timestamp(t.year, t.month, t.day, t.hour, t.minute,
284288
t.second)
285289
except ValueError as e:

xarray/core/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ def resample(self, freq=None, dim=None, how=None, skipna=None,
686686
"was passed %r" % dim)
687687

688688
if isinstance(self.indexes[dim_name], CFTimeIndex):
689-
raise TypeError(
689+
raise NotImplementedError(
690690
'Resample is currently not supported along a dimension '
691691
'indexed by a CFTimeIndex. For certain kinds of downsampling '
692692
'it may be possible to work around this by converting your '
@@ -723,7 +723,7 @@ def _resample_immediately(self, freq, dim, how, skipna,
723723
FutureWarning, stacklevel=3)
724724

725725
if isinstance(self.indexes[dim], CFTimeIndex):
726-
raise TypeError(
726+
raise NotImplementedError(
727727
'Resample is currently not supported along a dimension '
728728
'indexed by a CFTimeIndex. For certain kinds of downsampling '
729729
'it may be possible to work around this by converting your '

xarray/core/options.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@ def _set_file_cache_maxsize(value):
3838
FILE_CACHE.maxsize = value
3939

4040

41+
def _warn_on_setting_enable_cftimeindex(enable_cftimeindex):
42+
warnings.warn(
43+
'The enable_cftimeindex option is now a no-op '
44+
'and will be removed in a future version of xarray.',
45+
FutureWarning)
46+
47+
4148
_SETTERS = {
4249
FILE_CACHE_MAXSIZE: _set_file_cache_maxsize,
50+
ENABLE_CFTIMEINDEX: _warn_on_setting_enable_cftimeindex
4351
}
4452

4553

@@ -52,9 +60,6 @@ class set_options(object):
5260
Default: ``80``.
5361
- ``arithmetic_join``: DataArray/Dataset alignment in binary operations.
5462
Default: ``'inner'``.
55-
- ``enable_cftimeindex``: flag to enable using a ``CFTimeIndex``
56-
for time indexes with non-standard calendars or dates outside the
57-
Timestamp-valid range. Default: ``True``.
5863
- ``file_cache_maxsize``: maximum number of open files to hold in xarray's
5964
global least-recently-usage cached. This should be smaller than your
6065
system's per-process file descriptor limit, e.g., ``ulimit -n`` on Linux.
@@ -85,11 +90,6 @@ class set_options(object):
8590

8691
def __init__(self, **kwargs):
8792
self.old = OPTIONS.copy()
88-
if ENABLE_CFTIMEINDEX in kwargs:
89-
warnings.warn(
90-
'The enable_cftimeindex option is now a no-op '
91-
'and will be removed in a future version of xarray.',
92-
FutureWarning)
9393
for k, v in kwargs.items():
9494
if k not in OPTIONS:
9595
raise ValueError(

xarray/tests/test_cftimeindex.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def test_groupby(da):
361361

362362
@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
363363
def test_resample_error(da):
364-
with pytest.raises(TypeError):
364+
with pytest.raises(NotImplementedError, match='to_datetimeindex'):
365365
da.resample(time='Y')
366366

367367

@@ -749,11 +749,12 @@ def test_parse_array_of_cftime_strings():
749749

750750
@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
751751
@pytest.mark.parametrize('calendar', _ALL_CALENDARS)
752-
def test_to_datetimeindex(calendar):
752+
@pytest.mark.parametrize('unsafe', [False, True])
753+
def test_to_datetimeindex(calendar, unsafe):
753754
index = xr.cftime_range('2000', periods=5, calendar=calendar)
754755
expected = pd.date_range('2000', periods=5)
755756

756-
if calendar in _NON_STANDARD_CALENDARS:
757+
if calendar in _NON_STANDARD_CALENDARS and not unsafe:
757758
with pytest.warns(RuntimeWarning, match='non-standard'):
758759
result = index.to_datetimeindex()
759760
else:

xarray/tests/test_dataarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,7 @@ def test_resample_cftimeindex(self):
22812281
calendar='noleap')
22822282
array = DataArray(np.arange(12), [('time', times)])
22832283

2284-
with raises_regex(TypeError, 'to_datetimeindex'):
2284+
with raises_regex(NotImplementedError, 'to_datetimeindex'):
22852285
array.resample(time='6H').mean()
22862286

22872287
def test_resample_first(self):

xarray/tests/test_options.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ def test_arithmetic_join():
3131
def test_enable_cftimeindex():
3232
with pytest.raises(ValueError):
3333
xarray.set_options(enable_cftimeindex=None)
34-
with xarray.set_options(enable_cftimeindex=True):
35-
assert OPTIONS['enable_cftimeindex']
36-
with pytest.warns(FutureWarning):
34+
with pytest.warns(FutureWarning, match='no-op'):
3735
with xarray.set_options(enable_cftimeindex=True):
38-
pass
36+
assert OPTIONS['enable_cftimeindex']
3937

4038

4139
def test_file_cache_maxsize():

0 commit comments

Comments
 (0)