|
44 | 44 | import re |
45 | 45 | import warnings |
46 | 46 | from datetime import timedelta |
| 47 | +from distutils.version import LooseVersion |
47 | 48 |
|
48 | 49 | import numpy as np |
49 | 50 | import pandas as pd |
@@ -108,6 +109,11 @@ def _parse_iso8601_with_reso(date_type, timestr): |
108 | 109 | replace[attr] = int(value) |
109 | 110 | resolution = attr |
110 | 111 |
|
| 112 | + # dayofwk=-1 is required to update the dayofwk and dayofyr attributes of |
| 113 | + # the returned date object in versions of cftime between 1.0.2 and |
| 114 | + # 1.0.3.4. It can be removed for versions of cftime greater than |
| 115 | + # 1.0.3.4. |
| 116 | + replace['dayofwk'] = -1 |
111 | 117 | return default.replace(**replace), resolution |
112 | 118 |
|
113 | 119 |
|
@@ -150,11 +156,21 @@ def get_date_field(datetimes, field): |
150 | 156 | return np.array([getattr(date, field) for date in datetimes]) |
151 | 157 |
|
152 | 158 |
|
153 | | -def _field_accessor(name, docstring=None): |
| 159 | +def _field_accessor(name, docstring=None, min_cftime_version='0.0'): |
154 | 160 | """Adapted from pandas.tseries.index._field_accessor""" |
155 | 161 |
|
156 | | - def f(self): |
157 | | - return get_date_field(self._data, name) |
| 162 | + def f(self, min_cftime_version=min_cftime_version): |
| 163 | + import cftime |
| 164 | + |
| 165 | + version = cftime.__version__ |
| 166 | + |
| 167 | + if LooseVersion(version) >= LooseVersion(min_cftime_version): |
| 168 | + return get_date_field(self._data, name) |
| 169 | + else: |
| 170 | + raise ImportError('The {!r} accessor requires a minimum ' |
| 171 | + 'version of cftime of {}. Found an ' |
| 172 | + 'installed version of {}.'.format( |
| 173 | + name, min_cftime_version, version)) |
158 | 174 |
|
159 | 175 | f.__name__ = name |
160 | 176 | f.__doc__ = docstring |
@@ -209,8 +225,10 @@ class CFTimeIndex(pd.Index): |
209 | 225 | microsecond = _field_accessor('microsecond', |
210 | 226 | 'The microseconds of the datetime') |
211 | 227 | dayofyear = _field_accessor('dayofyr', |
212 | | - 'The ordinal day of year of the datetime') |
213 | | - dayofweek = _field_accessor('dayofwk', 'The day of week of the datetime') |
| 228 | + 'The ordinal day of year of the datetime', |
| 229 | + '1.0.2.1') |
| 230 | + dayofweek = _field_accessor('dayofwk', 'The day of week of the datetime', |
| 231 | + '1.0.2.1') |
214 | 232 | date_type = property(get_date_type) |
215 | 233 |
|
216 | 234 | def __new__(cls, data, name=None): |
|
0 commit comments