From 0af882ae157a6ef52bda6ea15ba4c8fb10f5d5b8 Mon Sep 17 00:00:00 2001 From: Alex Kleeman Date: Sun, 2 Mar 2014 23:14:35 -0800 Subject: [PATCH 1/2] During decode_cf do not encode any 'object' dtypes. When encoding cf variables check if dtype is np.datetime64. fixes akleeman/xray/issues/39 --- src/xray/conventions.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/xray/conventions.py b/src/xray/conventions.py index 773bd5bf188..0f5a0b2fa2f 100644 --- a/src/xray/conventions.py +++ b/src/xray/conventions.py @@ -249,11 +249,12 @@ def encode_cf_variable(array): attributes = array.attributes.copy() encoding = array.encoding - if isinstance(data, pd.DatetimeIndex): + if (isinstance(data, pd.DatetimeIndex) or + str(data.dtype).startswith('datetime')): # DatetimeIndex objects need to be encoded into numeric arrays (data, units, calendar) = utils.datetimeindex2num(data, - units=encoding.get('units', None), - calendar=encoding.get('calendar', None)) + units=encoding.pop('units', None), + calendar=encoding.pop('calendar', None)) attributes['units'] = units attributes['calendar'] = calendar elif data.dtype == np.dtype('O'): @@ -327,7 +328,10 @@ def pop_to(source, dest, k): if 'dtype' in encoding: if var.data.dtype != encoding['dtype']: raise ValueError("Refused to overwrite dtype") - encoding['dtype'] = data.dtype + if not isinstance(data, pd.Index): + # When data is a pandas Index we assume the dtype will be + # inferred during encode_cf_variable. + encoding['dtype'] = data.dtype if np.issubdtype(data.dtype, (str, unicode)): # TODO: add some sort of check instead of just assuming that the last # dimension on a character array is always the string dimension From 34b65e1af60b1740dd825b47ff80a0e50d0ade64 Mon Sep 17 00:00:00 2001 From: akleeman Date: Sun, 2 Mar 2014 23:52:14 -0800 Subject: [PATCH 2/2] Update conventions.py Changed datetime check, copied encoding. --- src/xray/conventions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/xray/conventions.py b/src/xray/conventions.py index 0f5a0b2fa2f..e7df7b5785c 100644 --- a/src/xray/conventions.py +++ b/src/xray/conventions.py @@ -247,10 +247,9 @@ def encode_cf_variable(array): dimensions = array.dimensions data = array.data attributes = array.attributes.copy() - encoding = array.encoding + encoding = array.encoding.copy() - if (isinstance(data, pd.DatetimeIndex) or - str(data.dtype).startswith('datetime')): + if np.issubdtype(data.dtype, np.datetime64): # DatetimeIndex objects need to be encoded into numeric arrays (data, units, calendar) = utils.datetimeindex2num(data, units=encoding.pop('units', None),