Skip to content

Commit 3ecfa66

Browse files
authored
Coordinate -> IndexVariable and other deprecations (#993)
* Coordinate -> IndexVariable and other deprecations - Renamed the ``Coordinate`` class from xarray's low level API to ``IndexVariable``. - Deprecated supplying ``coords`` as a dictionary to the ``DataArray`` constructor without also supplying an explicit ``dims`` argument. The old behavior encouraged relying on the iteration order of dictionaries, which is a bad practice (GH727). - Removed a number of methods deprecated since v0.7.0 or earlier: ``load_data``, ``vars``, ``drop_vars``, ``dump``, ``dumps`` and the ``variables`` keyword argument alias to ``Dataset``. * Rename to_coord() -> to_index_variable()
1 parent 1e31639 commit 3ecfa66

17 files changed

+181
-188
lines changed

doc/whats-new.rst

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ v0.9.0 (unreleased)
2121
Breaking changes
2222
~~~~~~~~~~~~~~~~
2323

24+
Deprecations
25+
~~~~~~~~~~~~
26+
27+
- Renamed the ``Coordinate`` class from xarray's low level API to
28+
:py:class:`~xarray.IndexVariable`. ``Variable.to_variable`` and
29+
``Variable.to_coord`` have been renamed to
30+
:py:meth:`~xarray.Variable.to_base_variable` and
31+
:py:meth:`~xarray.Variable.to_index_variable`.
32+
- Deprecated supplying ``coords`` as a dictionary to the ``DataArray``
33+
constructor without also supplying an explicit ``dims`` argument. The old
34+
behavior encouraged relying on the iteration order of dictionaries, which is
35+
a bad practice (:issue:`727`).
36+
- Removed a number of methods deprecated since v0.7.0 or earlier:
37+
``load_data``, ``vars``, ``drop_vars``, ``dump``, ``dumps`` and the
38+
``variables`` keyword argument to ``Dataset``.
39+
2440
Enhancements
2541
~~~~~~~~~~~~
2642

xarray/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .core.combine import concat, auto_combine
33
from .core.extensions import (register_dataarray_accessor,
44
register_dataset_accessor)
5-
from .core.variable import Variable, Coordinate
5+
from .core.variable import Variable, IndexVariable, Coordinate
66
from .core.dataset import Dataset
77
from .core.dataarray import DataArray
88
from .core.merge import merge, MergeError

xarray/core/alignment.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .common import _maybe_promote
1010
from .pycompat import iteritems, OrderedDict
1111
from .utils import is_full_slice, is_dict_like
12-
from .variable import Variable, Coordinate
12+
from .variable import Variable, IndexVariable
1313

1414

1515
def _get_joiner(join):
@@ -119,7 +119,7 @@ def reindex_variables(variables, indexes, indexers, method=None,
119119
variables : dict-like
120120
Dictionary of xarray.Variable objects.
121121
indexes : dict-like
122-
Dictionary of xarray.Coordinate objects associated with variables.
122+
Dictionary of xarray.IndexVariable objects associated with variables.
123123
indexers : dict
124124
Dictionary with keys given by dimension names and values given by
125125
arrays of coordinates tick labels. Any mis-matched coordinate values
@@ -200,8 +200,8 @@ def var_indexers(var, indexers):
200200
for name, var in iteritems(variables):
201201
if name in indexers:
202202
# no need to copy, because index data is immutable
203-
new_var = Coordinate(var.dims, indexers[name], var.attrs,
204-
var.encoding)
203+
new_var = IndexVariable(var.dims, indexers[name], var.attrs,
204+
var.encoding)
205205
else:
206206
assign_to = var_indexers(var, to_indexers)
207207
assign_from = var_indexers(var, from_indexers)

xarray/core/combine.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .alignment import align
77
from .merge import merge
88
from .pycompat import iteritems, OrderedDict, basestring
9-
from .variable import Variable, as_variable, Coordinate, concat as concat_vars
9+
from .variable import Variable, as_variable, IndexVariable, concat as concat_vars
1010

1111

1212
def concat(objs, dim=None, data_vars='all', coords='different',
@@ -125,14 +125,14 @@ def _calc_concat_dim_coord(dim):
125125
if isinstance(dim, basestring):
126126
coord = None
127127
elif not hasattr(dim, 'dims'):
128-
# dim is not a DataArray or Coordinate
128+
# dim is not a DataArray or IndexVariable
129129
dim_name = getattr(dim, 'name', None)
130130
if dim_name is None:
131131
dim_name = 'concat_dim'
132-
coord = Coordinate(dim_name, dim)
132+
coord = IndexVariable(dim_name, dim)
133133
dim = dim_name
134134
elif not hasattr(dim, 'name'):
135-
coord = as_variable(dim).to_coord()
135+
coord = as_variable(dim).to_index_variable()
136136
dim, = coord.dims
137137
else:
138138
coord = dim

xarray/core/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def groupby(self, group, squeeze=True):
326326
327327
Parameters
328328
----------
329-
group : str, DataArray or Coordinate
329+
group : str, DataArray or IndexVariable
330330
Array whose unique values should be used to group this array. If a
331331
string, must be the name of a variable contained in this dataset.
332332
squeeze : boolean, optional
@@ -353,7 +353,7 @@ def groupby_bins(self, group, bins, right=True, labels=None, precision=3,
353353
354354
Parameters
355355
----------
356-
group : str, DataArray or Coordinate
356+
group : str, DataArray or IndexVariable
357357
Array whose binned values should be used to group this array. If a
358358
string, must be the name of a variable contained in this dataset.
359359
bins : int or array of scalars

xarray/core/coordinates.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ class DataArrayCoordinates(AbstractCoordinates):
177177
"""Dictionary like container for DataArray coordinates.
178178
179179
Essentially an OrderedDict with keys given by the array's
180-
dimensions and the values given by the corresponding xarray.Coordinate
181-
objects.
180+
dimensions and the values given by corresponding DataArray objects.
182181
"""
183182
def __init__(self, dataarray):
184183
self._data = dataarray

xarray/core/dataarray.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .coordinates import DataArrayCoordinates, Indexes
1818
from .dataset import Dataset
1919
from .pycompat import iteritems, basestring, OrderedDict, zip
20-
from .variable import (as_variable, Variable, as_compatible_data, Coordinate,
20+
from .variable import (as_variable, Variable, as_compatible_data, IndexVariable,
2121
default_index_coordinate)
2222
from .formatting import format_item
2323

@@ -39,11 +39,14 @@ def _infer_coords_and_dims(shape, coords, dims):
3939
if coords is not None and len(coords) == len(shape):
4040
# try to infer dimensions from coords
4141
if utils.is_dict_like(coords):
42-
# TODO: deprecate this path
42+
warnings.warn('inferring DataArray dimensions from dictionary '
43+
'like ``coords`` has been deprecated. Use an '
44+
'explicit list of ``dims`` instead.',
45+
FutureWarning, stacklevel=3)
4346
dims = list(coords.keys())
4447
else:
4548
for n, (dim, coord) in enumerate(zip(dims, coords)):
46-
coord = as_variable(coord, name=dims[n]).to_coord()
49+
coord = as_variable(coord, name=dims[n]).to_index_variable()
4750
dims[n] = coord.name
4851
dims = tuple(dims)
4952
else:
@@ -142,7 +145,7 @@ class DataArray(AbstractArray, BaseDataObject):
142145
values : np.ndarray
143146
Access or modify DataArray values as a numpy array.
144147
coords : dict-like
145-
Dictionary of Coordinate objects that label values along each dimension.
148+
Dictionary of DataArray objects that label values along each dimension.
146149
name : str or None
147150
Name of this array.
148151
attrs : OrderedDict
@@ -197,7 +200,7 @@ def __init__(self, data, coords=None, dims=None, name=None,
197200
coords = [data.index]
198201
elif isinstance(data, pd.DataFrame):
199202
coords = [data.index, data.columns]
200-
elif isinstance(data, (pd.Index, Coordinate)):
203+
elif isinstance(data, (pd.Index, IndexVariable)):
201204
coords = [data]
202205
elif isinstance(data, pd.Panel):
203206
coords = [data.items, data.major_axis, data.minor_axis]
@@ -245,7 +248,7 @@ def _replace_indexes(self, indexes):
245248
return self
246249
coords = self._coords.copy()
247250
for name, idx in indexes.items():
248-
coords[name] = Coordinate(name, idx)
251+
coords[name] = IndexVariable(name, idx)
249252
obj = self._replace(coords=coords)
250253

251254
# switch from dimension to level names, if necessary
@@ -535,12 +538,6 @@ def load(self):
535538
self._coords = new._coords
536539
return self
537540

538-
def load_data(self): # pragma: no cover
539-
warnings.warn('the DataArray method `load_data` has been deprecated; '
540-
'use `load` instead',
541-
FutureWarning, stacklevel=2)
542-
return self.load()
543-
544541
def copy(self, deep=True):
545542
"""Returns a copy of this array.
546543

xarray/core/dataset.py

+10-37
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .merge import (dataset_update_method, dataset_merge_method,
2121
merge_data_and_coords)
2222
from .utils import Frozen, SortedKeysDict, maybe_wrap_array, hashable
23-
from .variable import (Variable, as_variable, Coordinate, broadcast_variables)
23+
from .variable import (Variable, as_variable, IndexVariable, broadcast_variables)
2424
from .pycompat import (iteritems, basestring, OrderedDict,
2525
dask_array_type)
2626
from .combine import concat
@@ -157,7 +157,7 @@ class Dataset(Mapping, ImplementsDatasetReduce, BaseDataObject,
157157
groupby_cls = groupby.DatasetGroupBy
158158

159159
def __init__(self, data_vars=None, coords=None, attrs=None,
160-
compat='broadcast_equals', **kwargs):
160+
compat='broadcast_equals'):
161161
"""To load data from a file or file-like object, use the `open_dataset`
162162
function.
163163
@@ -183,7 +183,7 @@ def __init__(self, data_vars=None, coords=None, attrs=None,
183183
Global attributes to save on this dataset.
184184
compat : {'broadcast_equals', 'equals', 'identical'}, optional
185185
String indicating how to compare variables of the same name for
186-
potential conflicts:
186+
potential conflicts when initializing this dataset:
187187
188188
- 'broadcast_equals': all values must be equal when variables are
189189
broadcast against each other to ensure common dimensions.
@@ -196,14 +196,6 @@ def __init__(self, data_vars=None, coords=None, attrs=None,
196196
self._dims = {}
197197
self._attrs = None
198198
self._file_obj = None
199-
if kwargs:
200-
if 'variables' in kwargs:
201-
data_vars = kwargs.pop('variables')
202-
warnings.warn('`variables` kwarg is deprecated. Use '
203-
'`data_vars` instead.', stacklevel=2)
204-
if kwargs:
205-
raise TypeError(
206-
'{0} are not valid kwargs'.format(kwargs.keys()))
207199
if data_vars is None:
208200
data_vars = {}
209201
if coords is None:
@@ -326,12 +318,6 @@ def load(self):
326318

327319
return self
328320

329-
def load_data(self): # pragma: no cover
330-
warnings.warn('the Dataset method `load_data` has been deprecated; '
331-
'use `load` instead',
332-
FutureWarning, stacklevel=2)
333-
return self.load()
334-
335321
@classmethod
336322
def _construct_direct(cls, variables, coord_names, dims=None, attrs=None,
337323
file_obj=None):
@@ -398,7 +384,7 @@ def _replace_indexes(self, indexes):
398384
return self
399385
variables = self._variables.copy()
400386
for name, idx in indexes.items():
401-
variables[name] = Coordinate(name, idx)
387+
variables[name] = IndexVariable(name, idx)
402388
obj = self._replace_vars_and_dims(variables)
403389

404390
# switch from dimension to level names, if necessary
@@ -641,13 +627,6 @@ def data_vars(self):
641627
"""
642628
return DataVariables(self)
643629

644-
@property
645-
def vars(self): # pragma: no cover
646-
warnings.warn('the Dataset property `vars` has been deprecated; '
647-
'use `data_vars` instead',
648-
FutureWarning, stacklevel=2)
649-
return self.data_vars
650-
651630
def set_coords(self, names, inplace=False):
652631
"""Given names of one or more variables, set them as coordinates
653632
@@ -781,9 +760,6 @@ def to_netcdf(self, path=None, mode='w', format=None, group=None,
781760
return to_netcdf(self, path, mode, format=format, group=group,
782761
engine=engine, encoding=encoding)
783762

784-
dump = utils.function_alias(to_netcdf, 'dump')
785-
dumps = utils.function_alias(to_netcdf, 'dumps')
786-
787763
def __unicode__(self):
788764
return formatting.dataset_repr(self)
789765

@@ -1303,7 +1279,10 @@ def swap_dims(self, dims_dict, inplace=False):
13031279

13041280
for k, v in iteritems(self.variables):
13051281
dims = tuple(dims_dict.get(dim, dim) for dim in v.dims)
1306-
var = v.to_coord() if k in result_dims else v.to_variable()
1282+
if k in result_dims:
1283+
var = v.to_index_variable()
1284+
else:
1285+
var = v.to_base_variable()
13071286
var.dims = dims
13081287
variables[k] = var
13091288

@@ -1326,7 +1305,7 @@ def _stack_once(self, dims, new_dim):
13261305

13271306
idx = utils.multiindex_from_product_levels(
13281307
[self.indexes[d] for d in dims], names=dims)
1329-
variables[new_dim] = Coordinate(new_dim, idx)
1308+
variables[new_dim] = IndexVariable(new_dim, idx)
13301309

13311310
coord_names = set(self._coord_names) - set(dims) | set([new_dim])
13321311

@@ -1404,7 +1383,7 @@ def unstack(self, dim):
14041383
variables[name] = var
14051384

14061385
for name, lev in zip(new_dim_names, index.levels):
1407-
variables[name] = Coordinate(name, lev)
1386+
variables[name] = IndexVariable(name, lev)
14081387

14091388
coord_names = set(self._coord_names) - set([dim]) | set(new_dim_names)
14101389

@@ -1533,12 +1512,6 @@ def _drop_vars(self, names):
15331512
coord_names = set(k for k in self._coord_names if k in variables)
15341513
return self._replace_vars_and_dims(variables, coord_names)
15351514

1536-
def drop_vars(self, *names): # pragma: no cover
1537-
warnings.warn('the Dataset method `drop_vars` has been deprecated; '
1538-
'use `drop` instead',
1539-
FutureWarning, stacklevel=2)
1540-
return self.drop(names)
1541-
15421515
def transpose(self, *dims):
15431516
"""Return a new Dataset object with all array dimensions transposed.
15441517

xarray/core/formatting.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def format_timestamp(t):
9999
datetime_str = unicode_type(pd.Timestamp(t))
100100
except OutOfBoundsDatetime:
101101
datetime_str = unicode_type(t)
102-
102+
103103
try:
104104
date_str, time_str = datetime_str.split()
105105
except ValueError:
@@ -271,7 +271,7 @@ def indexes_repr(indexes):
271271

272272

273273
def array_repr(arr):
274-
# used for DataArray, Variable and Coordinate
274+
# used for DataArray, Variable and IndexVariable
275275
if hasattr(arr, 'name') and arr.name is not None:
276276
name_str = '%r ' % arr.name
277277
else:

xarray/core/groupby.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)
1111
from .pycompat import zip
1212
from .utils import peek_at, maybe_wrap_array, safe_cast_to_index
13-
from .variable import as_variable, Variable, Coordinate
13+
from .variable import as_variable, Variable, IndexVariable
1414

1515

1616
def unique_value_groups(ar, sort=True):
@@ -63,6 +63,7 @@ def _dummy_copy(xarray_obj):
6363
dict((k, _get_fill_value(v.dtype))
6464
for k, v in xarray_obj.coords.items()
6565
if k not in xarray_obj.dims),
66+
dims=[],
6667
name=xarray_obj.name,
6768
attrs=xarray_obj.attrs)
6869
else: # pragma: no cover
@@ -140,7 +141,7 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
140141
----------
141142
obj : Dataset or DataArray
142143
Object to group.
143-
group : DataArray or Coordinate
144+
group : DataArray or IndexVariable
144145
1-dimensional array with the group values.
145146
squeeze : boolean, optional
146147
If "group" is a coordinate of object, `squeeze` controls whether
@@ -206,7 +207,7 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
206207
sbins = first_items.values.astype(np.int64)
207208
group_indices = ([slice(i, j) for i, j in zip(sbins[:-1], sbins[1:])] +
208209
[slice(sbins[-1], None)])
209-
unique_coord = Coordinate(group.name, first_items.index)
210+
unique_coord = IndexVariable(group.name, first_items.index)
210211
elif group.name in obj.dims and bins is None:
211212
# assume that group already has sorted, unique values
212213
# (if using bins, the group will have the same name as a dimension
@@ -224,7 +225,7 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
224225
# look through group to find the unique values
225226
sort = bins is None
226227
unique_values, group_indices = unique_value_groups(group, sort=sort)
227-
unique_coord = Coordinate(group.name, unique_values)
228+
unique_coord = IndexVariable(group.name, unique_values)
228229

229230
self.obj = obj
230231
self.group = group

0 commit comments

Comments
 (0)