Skip to content

Commit acee12f

Browse files
committed
Rename Index (formerly Coordinate) to XIndex
Fixes pydata#178
1 parent e2be878 commit acee12f

11 files changed

+77
-79
lines changed

test/test_data_array.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from copy import deepcopy
44
from textwrap import dedent
55

6-
from xray import Dataset, DataArray, Index, Variable, align
6+
from xray import Dataset, DataArray, XIndex, Variable, align
77
from xray.pycompat import iteritems, OrderedDict
88
from . import TestCase, ReturnItem, source_ndarray
99

@@ -250,7 +250,7 @@ def test_loc(self):
250250
self.assertTrue(np.all(da.values == 0))
251251

252252
def test_indexes(self):
253-
indexes = [Index('x', [-1, -2]), Index('y', [0, 1, 2])]
253+
indexes = [XIndex('x', [-1, -2]), XIndex('y', [0, 1, 2])]
254254
da = DataArray(np.random.randn(2, 3), indexes, name='foo')
255255

256256
self.assertEquals(2, len(da.indexes))

test/test_dataset.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def test_indexes_create(self):
136136
attributes = {'foo': 'bar'}
137137
a['x'] = ('x', vec, attributes)
138138
self.assertTrue('x' in a.indexes)
139-
self.assertIsInstance(a.indexes['x'].as_pandas, pd.Index)
139+
self.assertIsInstance(a.indexes['x'].as_index, pd.Index)
140140
self.assertVariableIdentical(a.indexes['x'], a.variables['x'])
141141
b = Dataset()
142142
b['x'] = ('x', vec, attributes)
@@ -467,7 +467,7 @@ def test_virtual_variables(self):
467467
self.assertVariableEqual(data['time.dayofyear'],
468468
Variable('time', 1 + np.arange(20)))
469469
self.assertArrayEqual(data['time.month'].values,
470-
data.variables['time'].as_pandas.month)
470+
data.variables['time'].as_index.month)
471471
self.assertArrayEqual(data['time.season'].values, 1)
472472
# test virtual variable math
473473
self.assertArrayEqual(data['time.dayofyear'] + 1, 2 + np.arange(20))
@@ -563,7 +563,7 @@ def test_groupby_errors(self):
563563
with self.assertRaisesRegexp(ValueError, 'length does not match'):
564564
data.groupby(data['dim1'][:3])
565565
with self.assertRaisesRegexp(ValueError, "must have a 'dimensions'"):
566-
data.groupby(data.indexes['dim1'].as_pandas)
566+
data.groupby(data.indexes['dim1'].as_index)
567567

568568
def test_groupby_reduce(self):
569569
data = Dataset({'xy': (['x', 'y'], np.random.randn(3, 4)),

test/test_indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
from xray import indexing, variable, Dataset, Variable, Index
3+
from xray import indexing, variable, Dataset, Variable, XIndex
44
from . import TestCase, ReturnItem
55

66

@@ -65,7 +65,7 @@ def test_orthogonal_indexer(self):
6565

6666
def test_convert_label_indexer(self):
6767
# TODO: add tests that aren't just for edge cases
68-
coord = Index('x', [1, 2, 3])
68+
coord = XIndex('x', [1, 2, 3])
6969
with self.assertRaisesRegexp(ValueError, 'not all values found'):
7070
indexing.convert_label_indexer(coord, [0])
7171
with self.assertRaises(KeyError):

test/test_variable.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88

99
from xray import Variable, Dataset, DataArray, indexing
10-
from xray.variable import (Index, as_variable, NumpyArrayAdapter,
10+
from xray.variable import (XIndex, as_variable, NumpyArrayAdapter,
1111
PandasIndexAdapter, _as_compatible_data)
1212
from xray.pycompat import PY3, OrderedDict
1313

@@ -171,9 +171,9 @@ def test_1d_math(self):
171171
self.assertEqual(float, (0 + v).values.dtype)
172172
# check types of returned data
173173
self.assertIsInstance(+v, Variable)
174-
self.assertNotIsInstance(+v, Index)
174+
self.assertNotIsInstance(+v, XIndex)
175175
self.assertIsInstance(0 + v, Variable)
176-
self.assertNotIsInstance(0 + v, Index)
176+
self.assertNotIsInstance(0 + v, XIndex)
177177

178178
def test_1d_reduce(self):
179179
x = np.arange(5)
@@ -194,7 +194,7 @@ def test_array_interface(self):
194194
# test ufuncs
195195
self.assertVariableIdentical(np.sin(v), self.cls(['x'], np.sin(x)))
196196
self.assertIsInstance(np.sin(v), Variable)
197-
self.assertNotIsInstance(np.sin(v), Index)
197+
self.assertNotIsInstance(np.sin(v), XIndex)
198198

199199
def example_1d_objects(self):
200200
for data in [range(3),
@@ -299,7 +299,7 @@ def test_numpy_same_methods(self):
299299
self.assertEqual(v.item(), 0)
300300
self.assertIs(type(v.item()), float)
301301

302-
v = Index('x', np.arange(5))
302+
v = XIndex('x', np.arange(5))
303303
self.assertEqual(2, v.searchsorted(2))
304304

305305
def test_datetime64_conversion(self):
@@ -566,27 +566,27 @@ def test_reduce_keep_attrs(self):
566566
self.assertEqual(vm.attrs, _attrs)
567567

568568

569-
class TestIndex(TestCase, VariableSubclassTestCases):
570-
cls = staticmethod(Index)
569+
class TestXIndex(TestCase, VariableSubclassTestCases):
570+
cls = staticmethod(XIndex)
571571

572572
def test_init(self):
573573
with self.assertRaisesRegexp(ValueError, 'must be 1-dimensional'):
574-
Index((), 0)
574+
XIndex((), 0)
575575

576576
def test_as_index(self):
577577
data = 0.5 * np.arange(10)
578-
v = Index(['time'], data, {'foo': 'bar'})
579-
self.assertTrue(pd.Index(data, name='time').identical(v.as_pandas))
578+
v = XIndex(['time'], data, {'foo': 'bar'})
579+
self.assertTrue(pd.Index(data, name='time').identical(v.as_index))
580580

581581
def test_data(self):
582-
x = Index('x', np.arange(3.0))
582+
x = XIndex('x', np.arange(3.0))
583583
# data should be initially saved as an ndarray
584584
self.assertIs(type(x._data), NumpyArrayAdapter)
585585
self.assertEqual(float, x.dtype)
586586
self.assertArrayEqual(np.arange(3), x)
587587
self.assertEqual(float, x.values.dtype)
588588
self.assertEqual('x', x.name)
589-
# after inspecting x.values, the Index will be saved as an Index
589+
# after inspecting x.values, the XIndex value will be saved as an Index
590590
self.assertIsInstance(x._data, PandasIndexAdapter)
591591
with self.assertRaisesRegexp(TypeError, 'cannot be modified'):
592592
x[:] = 0
@@ -595,7 +595,7 @@ def test_avoid_index_dtype_inference(self):
595595
# verify our work-around for (pandas<0.14):
596596
# https://github.com/pydata/pandas/issues/6370
597597
data = pd.date_range('2000-01-01', periods=3).to_pydatetime()
598-
t = Index('t', data)
598+
t = XIndex('t', data)
599599
self.assertArrayEqual(t.values[:2], data[:2])
600600
self.assertArrayEqual(t[:2].values, data[:2])
601601
self.assertArrayEqual(t.values[:2], data[:2])

xray/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .variable import Variable, Index, Coordinate
1+
from .variable import Variable, XIndex, Index, Coordinate
22
from .dataset import Dataset, open_dataset
33
from .data_array import DataArray, align
44

xray/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __contains__(self, key):
116116
return key in self._data.dimensions
117117

118118
def __repr__(self):
119-
return '\n'.join(_wrap_indent(repr(v.as_pandas), '%s: ' % k)
119+
return '\n'.join(_wrap_indent(repr(v.as_index), '%s: ' % k)
120120
for k, v in self.items())
121121

122122

xray/data_array.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def _infer_indexes_and_dimensions(shape, indexes, dimensions):
5454
if indexes is None:
5555
indexes = [None] * len(shape)
5656
indexes = [idx if isinstance(idx, AbstractArray) else
57-
variable.Index(dimensions[n], idx) if idx is not None else
58-
variable.Index(dimensions[n], np.arange(shape[n]))
57+
variable.XIndex(dimensions[n], idx) if idx is not None else
58+
variable.XIndex(dimensions[n], np.arange(shape[n]))
5959
for n, idx in enumerate(indexes)]
6060

6161
return indexes, dimensions
@@ -268,7 +268,7 @@ def _in_memory(self):
268268
def as_index(self):
269269
"""The variable's data as a pandas.Index. Only possible for 1D arrays.
270270
"""
271-
return self.variable.to_index().as_pandas
271+
return self.variable.to_xindex().as_index
272272

273273
@property
274274
def dimensions(self):
@@ -342,7 +342,7 @@ def coordinates(self):
342342

343343
@property
344344
def indexes(self):
345-
"""Dictionary-like container of xray.Index objects used for label based
345+
"""Dictionary-like container of xray.XIndex objects used for label based
346346
indexing.
347347
348348
Keys are given by the dimensions, but list-like (integer based)
@@ -441,7 +441,7 @@ def reindex_like(self, other, copy=True):
441441
return self.reindex(copy=copy, **other.indexes)
442442

443443
def reindex(self, copy=True, **indexes):
444-
"""Conform this object onto a new set of indxes or pandas.Index
444+
"""Conform this object onto a new set of indexes or pandas.Index
445445
objects, filling in missing values with NaN.
446446
447447
Parameters
@@ -524,7 +524,7 @@ def groupby(self, group, squeeze=True):
524524
525525
Parameters
526526
----------
527-
group : str, DataArray or Index
527+
group : str, DataArray or XIndex
528528
Array whose unique values should be used to group this array. If a
529529
string, must be the name of a variable contained in this dataset.
530530
squeeze : boolean, optional
@@ -895,7 +895,7 @@ def align(*objects, **kwargs):
895895
all_indexes = defaultdict(list)
896896
for obj in objects:
897897
for k, v in iteritems(obj.indexes):
898-
all_indexes[k].append(v.as_pandas)
898+
all_indexes[k].append(v.as_index)
899899

900900
# Exclude dimensions with all equal indices to avoid unnecessary reindexing
901901
# work.

xray/dataset.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _castable_to_timestamp(obj):
105105

106106
virtual_vars = []
107107
for k, v in iteritems(self):
108-
if ((v.dtype.kind == 'M' and isinstance(v, variable.Index))
108+
if ((v.dtype.kind == 'M' and isinstance(v, variable.XIndex))
109109
or (v.ndim == 0 and _castable_to_timestamp(v.values))):
110110
# nb. dtype.kind == 'M' is datetime64
111111
for suffix in _DATETIMEINDEX_COMPONENTS + ['season']:
@@ -126,8 +126,8 @@ def __missing__(self, key):
126126

127127
ref_var_name, suffix = split_key
128128
ref_var = self[ref_var_name]
129-
if isinstance(ref_var, variable.Index):
130-
date = ref_var.as_pandas
129+
if isinstance(ref_var, variable.XIndex):
130+
date = ref_var.as_index
131131
elif ref_var.ndim == 0:
132132
date = pd.Timestamp(ref_var.values)
133133

@@ -154,7 +154,7 @@ def _as_dataset_variable(name, var):
154154
if var.ndim != 1:
155155
raise ValueError('an index variable must be defined with '
156156
'1-dimensional data')
157-
var = var.to_index()
157+
var = var.to_xindex()
158158
return var
159159

160160

@@ -165,8 +165,8 @@ def _expand_variables(raw_variables, old_variables={}, compat='identical'):
165165
Dataset._variables dictionary.
166166
167167
This includes converting tuples (dimensions, data) into Variable objects,
168-
converting index variables into Index objects and expanding DataArray
169-
objects into Variables plus Indexs.
168+
converting index variables into XIndex objects and expanding DataArray
169+
objects into Variables plus XIndexes.
170170
171171
Raises ValueError if any conflicting values are found, between any of the
172172
new or old variables.
@@ -277,7 +277,7 @@ class Dataset(Mapping, common.ImplementsDatasetReduce):
277277
and values given by DataArray objects for each variable name.
278278
279279
One dimensional variables with name equal to their dimension are indexes,
280-
which means they are saved in the dataset as `xray.Index` objects.
280+
which means they are saved in the dataset as `xray.XIndex` objects.
281281
"""
282282
def __init__(self, variables=None, attributes=None):
283283
"""To load data from a file or file-like object, use the `open_dataset`
@@ -307,7 +307,7 @@ def _add_missing_indexes(self):
307307
"""
308308
for dim, size in iteritems(self._dimensions):
309309
if dim not in self._variables:
310-
coord = variable.Index(dim, np.arange(size))
310+
coord = variable.XIndex(dim, np.arange(size))
311311
self._variables[dim] = coord
312312

313313
def _update_vars_and_dims(self, new_variables, needs_copy=True):
@@ -790,8 +790,8 @@ def get_fill_value_and_dtype(dtype):
790790
new_var = indexes[name]
791791
if not (hasattr(new_var, 'dimensions') and
792792
hasattr(new_var, 'values')):
793-
new_var = variable.Index(var.dimensions, new_var,
794-
var.attrs, var.encoding)
793+
new_var = variable.XIndex(var.dimensions, new_var,
794+
var.attrs, var.encoding)
795795
elif copy:
796796
new_var = variable.as_variable(new_var).copy()
797797
else:

xray/groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .common import ImplementsArrayReduce, ImplementsDatasetReduce
88
from .ops import inject_reduce_methods
9-
from .variable import as_variable, Variable, Index
9+
from .variable import as_variable, Variable, XIndex
1010
import xray
1111
import numpy as np
1212

@@ -66,7 +66,7 @@ def __init__(self, obj, group, squeeze=True):
6666
----------
6767
obj : Dataset or DataArray
6868
Object to group.
69-
group : DataArray or Index
69+
group : DataArray or XIndex
7070
1-dimensional array with the group values.
7171
squeeze : boolean, optional
7272
If "group" is a coordinate of object, `squeeze` controls whether
@@ -106,7 +106,7 @@ def __init__(self, obj, group, squeeze=True):
106106
else:
107107
# look through group to find the unique values
108108
unique_values, group_indices = unique_value_groups(group)
109-
unique_index = Index(group.name, unique_values)
109+
unique_index = XIndex(group.name, unique_values)
110110

111111
self.group_indices = group_indices
112112
self.unique_index = unique_index

xray/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def safe_cast_to_index(array):
9191
"""
9292
if isinstance(array, pd.Index):
9393
index = array
94-
elif isinstance(array, xray.Index):
95-
index = array.as_pandas
94+
elif isinstance(array, xray.XIndex):
95+
index = array.as_index
9696
else:
9797
kwargs = {}
9898
if hasattr(array, 'dtype'):

0 commit comments

Comments
 (0)