Skip to content

Commit e258b88

Browse files
andersy005dcherian
authored andcommitted
Sync with latest version of cftime (v1.0.4) (#3430)
* Remove `dayofwk=-1`-- not needed for cftime>=1.0.4 * Maintain backwards compatibility Co-Authored-By: Spencer Clark <[email protected]> * Maintain backwards compatibility Co-Authored-By: Spencer Clark <[email protected]> * Maintain backwards compatibility * Add missing import * Update whats-new
1 parent 02c2969 commit e258b88

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Bug fixes
4141
- Fix regression introduced in v0.14.0 that would cause a crash if dask is installed
4242
but cloudpickle isn't (:issue:`3401`) by `Rhys Doyle <https://github.com/rdoyle45>`_
4343

44+
- Sync with cftime by removing `dayofwk=-1` for cftime>=1.0.4.
45+
By `Anderson Banihirwe <https://github.com/andersy005>`_.
46+
47+
4448
Documentation
4549
~~~~~~~~~~~~~
4650

xarray/coding/cftime_offsets.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from ..core.pdcompat import count_not_none
5151
from .cftimeindex import CFTimeIndex, _parse_iso8601_with_reso
5252
from .times import format_cftime_datetime
53+
from distutils.version import LooseVersion
5354

5455

5556
def get_date_type(calendar):
@@ -222,6 +223,8 @@ def _adjust_n_years(other, n, month, reference_day):
222223
def _shift_month(date, months, day_option="start"):
223224
"""Shift the date to a month start or end a given number of months away.
224225
"""
226+
import cftime
227+
225228
delta_year = (date.month + months) // 12
226229
month = (date.month + months) % 12
227230

@@ -237,11 +240,14 @@ def _shift_month(date, months, day_option="start"):
237240
day = _days_in_month(reference)
238241
else:
239242
raise ValueError(day_option)
240-
# dayofwk=-1 is required to update the dayofwk and dayofyr attributes of
241-
# the returned date object in versions of cftime between 1.0.2 and
242-
# 1.0.3.4. It can be removed for versions of cftime greater than
243-
# 1.0.3.4.
244-
return date.replace(year=year, month=month, day=day, dayofwk=-1)
243+
if LooseVersion(cftime.__version__) < LooseVersion("1.0.4"):
244+
# dayofwk=-1 is required to update the dayofwk and dayofyr attributes of
245+
# the returned date object in versions of cftime between 1.0.2 and
246+
# 1.0.3.4. It can be removed for versions of cftime greater than
247+
# 1.0.3.4.
248+
return date.replace(year=year, month=month, day=day, dayofwk=-1)
249+
else:
250+
return date.replace(year=year, month=month, day=day)
245251

246252

247253
def roll_qtrday(other, n, month, day_option, modby=3):

xarray/coding/cftimeindex.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def parse_iso8601(datetime_string):
9696

9797

9898
def _parse_iso8601_with_reso(date_type, timestr):
99+
import cftime
100+
99101
default = date_type(1, 1, 1)
100102
result = parse_iso8601(timestr)
101103
replace = {}
@@ -107,12 +109,12 @@ def _parse_iso8601_with_reso(date_type, timestr):
107109
# TODO: Consider adding support for sub-second resolution?
108110
replace[attr] = int(value)
109111
resolution = attr
110-
111-
# dayofwk=-1 is required to update the dayofwk and dayofyr attributes of
112-
# the returned date object in versions of cftime between 1.0.2 and
113-
# 1.0.3.4. It can be removed for versions of cftime greater than
114-
# 1.0.3.4.
115-
replace["dayofwk"] = -1
112+
if LooseVersion(cftime.__version__) < LooseVersion("1.0.4"):
113+
# dayofwk=-1 is required to update the dayofwk and dayofyr attributes of
114+
# the returned date object in versions of cftime between 1.0.2 and
115+
# 1.0.3.4. It can be removed for versions of cftime greater than
116+
# 1.0.3.4.
117+
replace["dayofwk"] = -1
116118
return default.replace(**replace), resolution
117119

118120

0 commit comments

Comments
 (0)