-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix datetime_to_numeric and Variable._to_numeric #2668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
9acf580
0a2b0c6
a4fceba
fca735c
b262437
96422e3
a7129d1
24d72bc
8c1fd0a
c319da6
63350ee
a49052c
003ac39
552aa94
0b26615
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| from .coordinates import ( | ||
| DatasetCoordinates, LevelCoordinatesSource, | ||
| assert_coordinate_consistent, remap_label_indexers) | ||
| from .duck_array_ops import datetime_to_numeric | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Within |
||
| from .indexes import Indexes, default_indexes | ||
| from .merge import ( | ||
| dataset_merge_method, dataset_update_method, merge_data_and_coords, | ||
|
|
@@ -32,9 +33,9 @@ | |
| from .pycompat import ( | ||
| OrderedDict, basestring, dask_array_type, iteritems, range) | ||
| from .utils import ( | ||
| Frozen, SortedKeysDict, _check_inplace, datetime_to_numeric, | ||
| decode_numpy_dict_values, either_dict_or_kwargs, ensure_us_time_resolution, | ||
| hashable, maybe_wrap_array) | ||
| Frozen, SortedKeysDict, _check_inplace, decode_numpy_dict_values, | ||
| either_dict_or_kwargs, ensure_us_time_resolution, hashable, | ||
| maybe_wrap_array) | ||
| from .variable import IndexVariable, Variable, as_variable, broadcast_variables | ||
|
|
||
| # list of attributes of pd.DatetimeIndex that are ndarrays of time info | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -279,14 +279,51 @@ def f(values, axis=None, skipna=None, **kwargs): | |
| _mean = _create_nan_agg_method('mean') | ||
|
|
||
|
|
||
| def datetime_to_numeric(array, offset=None, datetime_unit=None, dtype=float): | ||
| """Convert an array containing datetime-like data to an array of floats. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| da : array | ||
| Input data | ||
| offset: Scalar with the same type of array or None | ||
| If None, subtract minimum values to reduce round off error | ||
| datetime_unit: None or any of {'Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', | ||
| 'us', 'ns', 'ps', 'fs', 'as'} | ||
| dtype: target dtype | ||
|
|
||
| Returns | ||
| ------- | ||
| array | ||
| """ | ||
| if offset is None: | ||
| offset = array.min() | ||
| array = array - offset | ||
|
spencerkclark marked this conversation as resolved.
|
||
|
|
||
| if datetime_unit: | ||
| array = array / np.timedelta64(1, datetime_unit) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to move this to after we potentially convert an array of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To explain this a little more -- for cftime dates, |
||
|
|
||
| if not hasattr(array, 'dtype'): # scalar is converted to 0d-array | ||
| array = np.array(array) | ||
|
|
||
| if array.dtype.kind in 'O': | ||
| # possibly convert object array containing datetime.datetime | ||
| array = np.asarray(pd.Series(array.ravel())).reshape(array.shape) | ||
|
|
||
| # convert np.NaT to np.nan | ||
| if array.dtype.kind in 'mM': | ||
| return np.where(isnull(array), np.nan, array.astype(dtype)) | ||
| return array.astype(dtype) | ||
|
|
||
|
|
||
| def mean(array, axis=None, skipna=None, **kwargs): | ||
| """ inhouse mean that can handle datatime dtype """ | ||
| array = asarray(array) | ||
| if array.dtype.kind == 'M': | ||
| if array.dtype.kind in 'Mm': | ||
| offset = min(array) | ||
| # xarray always uses datetime[ns] for datetime | ||
| dtype = 'timedelta64[ns]' | ||
| return _mean(utils.datetime_to_numeric(array, offset), axis=axis, | ||
| return _mean(datetime_to_numeric(array, offset), axis=axis, | ||
| skipna=skipna, **kwargs).astype(dtype) + offset | ||
| else: | ||
| return _mean(array, axis=axis, skipna=skipna, **kwargs) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -573,3 +573,16 @@ def test_cftime_to_non_cftime_error(): | |
|
|
||
| with pytest.raises(TypeError): | ||
| da.interp(time=0.5) | ||
|
|
||
|
|
||
| @requires_cftime | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, please remove this decorator too. |
||
| @requires_scipy | ||
| def test_cftime_interp(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think you can find a better name for this test? It does not seem to use cftime. |
||
| a = xr.DataArray( | ||
| np.arange(21).reshape(3, 7), dims=['x', 'time'], | ||
| coords={'x': [1, 2, 3], | ||
| 'time': pd.date_range('01-01-2001', periods=7, freq='D')}) | ||
| xi = xr.DataArray( | ||
| np.linspace(1, 3, 50), dims=['time'], | ||
| coords={'time': pd.date_range('01-01-2001', periods=50, freq='H')}) | ||
| a.interp(x=xi, time=xi.time) # should no error | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @dcherian caught this bug before it made it into a release, so maybe we do not need a bug-fix note?