Skip to content

Commit ea0e7d7

Browse files
committed
Fix pandas < v0.15.2 and GH700
1 parent 4056704 commit ea0e7d7

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

xray/backends/api.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@ def maybe_decode_store(store, lock=False):
170170
else:
171171
file_arg = filename_or_obj
172172
token = tokenize(file_arg, group, decode_cf, mask_and_scale,
173-
decode_times, concat_characters,
174-
decode_coords, engine, chunks, lock,
175-
drop_variables)
173+
decode_times, concat_characters, decode_coords,
174+
engine, chunks, drop_variables)
176175
name_prefix = '%s:%s/' % (filename_or_obj, group or '')
177176
ds2 = ds.chunk(chunks, name_prefix=name_prefix, token=token,
178177
lock=lock)

xray/core/indexing.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,11 @@ class PandasIndexAdapter(utils.NDArrayMixin):
386386
def __init__(self, array, dtype=None):
387387
self.array = utils.safe_cast_to_index(array)
388388
if dtype is None:
389-
# if a PeriodIndex, force an object dtype
390389
if isinstance(array, pd.PeriodIndex):
391390
dtype = np.dtype('O')
391+
elif hasattr(array, 'categories'):
392+
# category isn't a real numpy dtype
393+
dtype = array.categories.dtype
392394
else:
393395
dtype = array.dtype
394396
self._dtype = dtype
@@ -405,7 +407,12 @@ def __array__(self, dtype=None):
405407
with suppress(AttributeError):
406408
# this might not be public API
407409
array = array.asobject
408-
return array.values.astype(dtype)
410+
return np.asarray(array.values, dtype=dtype)
411+
412+
@property
413+
def shape(self):
414+
# .shape is broken on pandas prior to v0.15.2
415+
return (len(self.array),)
409416

410417
def __getitem__(self, key):
411418
if isinstance(key, tuple) and len(key) == 1:

xray/test/test_dataarray.py

+9
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,15 @@ def test_to_and_from_series(self):
14131413
self.assertDataArrayIdentical(expected_da,
14141414
DataArray.from_series(actual))
14151415

1416+
def test_series_categorical_index(self):
1417+
# regression test for GH700
1418+
if not hasattr(pd, 'CategoricalIndex'):
1419+
raise unittest.SkipTest('requires pandas with CategoricalIndex')
1420+
1421+
s = pd.Series(range(5), index=pd.CategoricalIndex(list('aabbc')))
1422+
arr = DataArray(s)
1423+
assert "'a'" in repr(arr) # should not error
1424+
14161425
def test_to_masked_array(self):
14171426
rs = np.random.RandomState(44)
14181427
x = rs.random_sample(size=(10, 20))

xray/test/test_variable.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,8 @@ def test_datetime64_conversion(self):
163163
self.assertArrayEqual(v.values, times.values)
164164
self.assertEqual(v.values.dtype, np.dtype('datetime64[ns]'))
165165
same_source = source_ndarray(v.values) is source_ndarray(values)
166-
if preserve_source and self.cls is Variable:
166+
if preserve_source:
167167
self.assertTrue(same_source)
168-
else:
169-
self.assertFalse(same_source)
170168

171169
def test_timedelta64_conversion(self):
172170
times = pd.timedelta_range(start=0, periods=3)
@@ -181,10 +179,8 @@ def test_timedelta64_conversion(self):
181179
self.assertArrayEqual(v.values, times.values)
182180
self.assertEqual(v.values.dtype, np.dtype('timedelta64[ns]'))
183181
same_source = source_ndarray(v.values) is source_ndarray(values)
184-
if preserve_source and self.cls is Variable:
182+
if preserve_source:
185183
self.assertTrue(same_source)
186-
else:
187-
self.assertFalse(same_source)
188184

189185
def test_object_conversion(self):
190186
data = np.arange(5).astype(str).astype(object)

0 commit comments

Comments
 (0)