diff --git a/src/xray/conventions.py b/src/xray/conventions.py index 773bd5bf188..e7df7b5785c 100644 --- a/src/xray/conventions.py +++ b/src/xray/conventions.py @@ -247,13 +247,13 @@ 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): + if np.issubdtype(data.dtype, np.datetime64): # 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 +327,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