Skip to content

Commit 725bd57

Browse files
authored
More support of non-string dimension names (#2373)
* More support for non-string dimension. * Avoid using kwargs in rolling. * Restore assign_coords, fixes typo
1 parent 0b9ab2d commit 725bd57

File tree

5 files changed

+162
-95
lines changed

5 files changed

+162
-95
lines changed

xarray/core/common.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from . import duck_array_ops, dtypes, formatting, ops
1111
from .arithmetic import SupportsArithmetic
1212
from .pycompat import OrderedDict, basestring, dask_array_type, suppress
13-
from .utils import Frozen, SortedKeysDict
13+
from .utils import either_dict_or_kwargs, Frozen, SortedKeysDict
1414

1515

1616
class ImplementsArrayReduce(object):
@@ -526,24 +526,24 @@ def groupby_bins(self, group, bins, right=True, labels=None, precision=3,
526526
'precision': precision,
527527
'include_lowest': include_lowest})
528528

529-
def rolling(self, min_periods=None, center=False, **windows):
529+
def rolling(self, dim=None, min_periods=None, center=False, **dim_kwargs):
530530
"""
531531
Rolling window object.
532532
533533
Parameters
534534
----------
535+
dim: dict, optional
536+
Mapping from the dimension name to create the rolling iterator
537+
along (e.g. `time`) to its moving window size.
535538
min_periods : int, default None
536539
Minimum number of observations in window required to have a value
537540
(otherwise result is NA). The default, None, is equivalent to
538541
setting min_periods equal to the size of the window.
539542
center : boolean, default False
540543
Set the labels at the center of the window.
541-
**windows : dim=window
542-
dim : str
543-
Name of the dimension to create the rolling iterator
544-
along (e.g., `time`).
545-
window : int
546-
Size of the moving window.
544+
**dim_kwargs : optional
545+
The keyword arguments form of ``dim``.
546+
One of dim or dim_kwarg must be provided.
547547
548548
Returns
549549
-------
@@ -582,9 +582,9 @@ def rolling(self, min_periods=None, center=False, **windows):
582582
core.rolling.DataArrayRolling
583583
core.rolling.DatasetRolling
584584
"""
585-
586-
return self._rolling_cls(self, min_periods=min_periods,
587-
center=center, **windows)
585+
dim = either_dict_or_kwargs(dim, dim_kwargs, 'rolling')
586+
return self._rolling_cls(self, dim, min_periods=min_periods,
587+
center=center)
588588

589589
def resample(self, freq=None, dim=None, how=None, skipna=None,
590590
closed=None, label=None, base=0, keep_attrs=False, **indexer):
@@ -650,6 +650,8 @@ def resample(self, freq=None, dim=None, how=None, skipna=None,
650650
651651
.. [1] http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
652652
"""
653+
# TODO support non-string indexer after removing the old API.
654+
653655
from .dataarray import DataArray
654656
from .resample import RESAMPLE_DIM
655657

xarray/core/dataarray.py

+36-19
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,9 @@ def sel(self, indexers=None, method=None, tolerance=None, drop=False,
779779
DataArray.isel
780780
781781
"""
782-
indexers = either_dict_or_kwargs(indexers, indexers_kwargs, 'sel')
783782
ds = self._to_temp_dataset().sel(
784-
indexers=indexers, drop=drop, method=method, tolerance=tolerance)
783+
indexers=indexers, drop=drop, method=method, tolerance=tolerance,
784+
**indexers_kwargs)
785785
return self._from_temp_dataset(ds)
786786

787787
def isel_points(self, dim='points', **indexers):
@@ -1092,22 +1092,26 @@ def expand_dims(self, dim, axis=None):
10921092
ds = self._to_temp_dataset().expand_dims(dim, axis)
10931093
return self._from_temp_dataset(ds)
10941094

1095-
def set_index(self, append=False, inplace=False, **indexes):
1095+
def set_index(self, indexes=None, append=False, inplace=False,
1096+
**indexes_kwargs):
10961097
"""Set DataArray (multi-)indexes using one or more existing
10971098
coordinates.
10981099
10991100
Parameters
11001101
----------
1102+
indexes : {dim: index, ...}
1103+
Mapping from names matching dimensions and values given
1104+
by (lists of) the names of existing coordinates or variables to set
1105+
as new (multi-)index.
11011106
append : bool, optional
11021107
If True, append the supplied index(es) to the existing index(es).
11031108
Otherwise replace the existing index(es) (default).
11041109
inplace : bool, optional
11051110
If True, set new index(es) in-place. Otherwise, return a new
11061111
DataArray object.
1107-
**indexes : {dim: index, ...}
1108-
Keyword arguments with names matching dimensions and values given
1109-
by (lists of) the names of existing coordinates or variables to set
1110-
as new (multi-)index.
1112+
**indexes_kwargs: optional
1113+
The keyword arguments form of ``indexes``.
1114+
One of indexes or indexes_kwargs must be provided.
11111115
11121116
Returns
11131117
-------
@@ -1118,6 +1122,7 @@ def set_index(self, append=False, inplace=False, **indexes):
11181122
--------
11191123
DataArray.reset_index
11201124
"""
1125+
indexes = either_dict_or_kwargs(indexes, indexes_kwargs, 'set_index')
11211126
coords, _ = merge_indexes(indexes, self._coords, set(), append=append)
11221127
if inplace:
11231128
self._coords = coords
@@ -1156,25 +1161,31 @@ def reset_index(self, dims_or_levels, drop=False, inplace=False):
11561161
else:
11571162
return self._replace(coords=coords)
11581163

1159-
def reorder_levels(self, inplace=False, **dim_order):
1164+
def reorder_levels(self, dim_order=None, inplace=False,
1165+
**dim_order_kwargs):
11601166
"""Rearrange index levels using input order.
11611167
11621168
Parameters
11631169
----------
1170+
dim_order : optional
1171+
Mapping from names matching dimensions and values given
1172+
by lists representing new level orders. Every given dimension
1173+
must have a multi-index.
11641174
inplace : bool, optional
11651175
If True, modify the dataarray in-place. Otherwise, return a new
11661176
DataArray object.
1167-
**dim_order : optional
1168-
Keyword arguments with names matching dimensions and values given
1169-
by lists representing new level orders. Every given dimension
1170-
must have a multi-index.
1177+
**dim_order_kwargs: optional
1178+
The keyword arguments form of ``dim_order``.
1179+
One of dim_order or dim_order_kwargs must be provided.
11711180
11721181
Returns
11731182
-------
11741183
obj : DataArray
11751184
Another dataarray, with this dataarray's data but replaced
11761185
coordinates.
11771186
"""
1187+
dim_order = either_dict_or_kwargs(dim_order, dim_order_kwargs,
1188+
'reorder_levels')
11781189
replace_coords = {}
11791190
for dim, order in dim_order.items():
11801191
coord = self._coords[dim]
@@ -1190,7 +1201,7 @@ def reorder_levels(self, inplace=False, **dim_order):
11901201
else:
11911202
return self._replace(coords=coords)
11921203

1193-
def stack(self, **dimensions):
1204+
def stack(self, dimensions=None, **dimensions_kwargs):
11941205
"""
11951206
Stack any number of existing dimensions into a single new dimension.
11961207
@@ -1199,9 +1210,12 @@ def stack(self, **dimensions):
11991210
12001211
Parameters
12011212
----------
1202-
**dimensions : keyword arguments of the form new_name=(dim1, dim2, ...)
1213+
dimensions : Mapping of the form new_name=(dim1, dim2, ...)
12031214
Names of new dimensions, and the existing dimensions that they
12041215
replace.
1216+
**dimensions_kwargs:
1217+
The keyword arguments form of ``dimensions``.
1218+
One of dimensions or dimensions_kwargs must be provided.
12051219
12061220
Returns
12071221
-------
@@ -1230,7 +1244,7 @@ def stack(self, **dimensions):
12301244
--------
12311245
DataArray.unstack
12321246
"""
1233-
ds = self._to_temp_dataset().stack(**dimensions)
1247+
ds = self._to_temp_dataset().stack(dimensions, **dimensions_kwargs)
12341248
return self._from_temp_dataset(ds)
12351249

12361250
def unstack(self, dim):
@@ -1978,7 +1992,7 @@ def diff(self, dim, n=1, label='upper'):
19781992
ds = self._to_temp_dataset().diff(n=n, dim=dim, label=label)
19791993
return self._from_temp_dataset(ds)
19801994

1981-
def shift(self, **shifts):
1995+
def shift(self, shifts=None, **shifts_kwargs):
19821996
"""Shift this array by an offset along one or more dimensions.
19831997
19841998
Only the data is moved; coordinates stay in place. Values shifted from
@@ -1987,10 +2001,13 @@ def shift(self, **shifts):
19872001
19882002
Parameters
19892003
----------
1990-
**shifts : keyword arguments of the form {dim: offset}
2004+
shifts : Mapping with the form of {dim: offset}
19912005
Integer offset to shift along each of the given dimensions.
19922006
Positive offsets shift to the right; negative offsets shift to the
19932007
left.
2008+
**shifts_kwargs:
2009+
The keyword arguments form of ``shifts``.
2010+
One of shifts or shifts_kwarg must be provided.
19942011
19952012
Returns
19962013
-------
@@ -2012,8 +2029,8 @@ def shift(self, **shifts):
20122029
Coordinates:
20132030
* x (x) int64 0 1 2
20142031
"""
2015-
variable = self.variable.shift(**shifts)
2016-
return self._replace(variable)
2032+
ds = self._to_temp_dataset().shift(shifts=shifts, **shifts_kwargs)
2033+
return self._from_temp_dataset(ds)
20172034

20182035
def roll(self, shifts=None, roll_coords=None, **shifts_kwargs):
20192036
"""Roll this array by an offset along one or more dimensions.

0 commit comments

Comments
 (0)