Skip to content

Commit 8e3f188

Browse files
committed
Fix pandas < v0.15.2 and GH700
1 parent 9ad4773 commit 8e3f188

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

xray/backends/api.py

Lines changed: 2 additions & 3 deletions
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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,10 @@ 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')
392391
elif hasattr(array, 'categories'):
392+
# category isn't a real numpy dtype
393393
dtype = array.categories.dtype
394394
elif not utils.is_valid_numpy_dtype(array.dtype):
395395
dtype = np.dtype('O')
@@ -409,7 +409,12 @@ def __array__(self, dtype=None):
409409
with suppress(AttributeError):
410410
# this might not be public API
411411
array = array.asobject
412-
return np.asarray(array, dtype)
412+
return np.asarray(array.values, dtype=dtype)
413+
414+
@property
415+
def shape(self):
416+
# .shape is broken on pandas prior to v0.15.2
417+
return (len(self.array),)
413418

414419
def __getitem__(self, key):
415420
if isinstance(key, tuple) and len(key) == 1:

xray/test/test_dataarray.py

Lines changed: 9 additions & 0 deletions
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))

0 commit comments

Comments
 (0)