@@ -103,7 +103,7 @@ def calculate_dimensions(variables):
103
103
return dims
104
104
105
105
106
- def merge_indexes (indexers , variables , coord_names , append = False ):
106
+ def merge_indexes (indexes , variables , coord_names , append = False ):
107
107
"""Merge variables into multi-indexes.
108
108
109
109
Not public API. Used in Dataset and DataArray set_index
@@ -112,23 +112,24 @@ def merge_indexes(indexers, variables, coord_names, append=False):
112
112
vars_to_replace = {}
113
113
vars_to_remove = []
114
114
115
- for dim , var_names in indexers .items ():
115
+ for dim , var_names in indexes .items ():
116
116
if isinstance (var_names , basestring ):
117
117
var_names = [var_names ]
118
118
119
- names = []
120
- arrays = []
119
+ names , labels , levels = [], [], []
121
120
current_index_variable = variables [dim ]
122
121
123
122
if append :
124
123
current_index = current_index_variable .to_index ()
125
124
if isinstance (current_index , pd .MultiIndex ):
126
125
names .extend (current_index .names )
127
- for i in range (current_index .nlevels ):
128
- arrays . append (current_index .get_level_values ( i ) )
126
+ labels . extend (current_index .labels )
127
+ levels . extend (current_index .levels )
129
128
else :
130
129
names .append ('%s_level_0' % dim )
131
- arrays .append (current_index .values )
130
+ cat = pd .Categorical (current_index .values , ordered = True )
131
+ labels .append (cat .codes )
132
+ levels .append (cat .categories )
132
133
133
134
for n in var_names :
134
135
names .append (n )
@@ -138,9 +139,11 @@ def merge_indexes(indexers, variables, coord_names, append=False):
138
139
"dimension mismatch between %r %s and %r %s"
139
140
% (dim , current_index_variable .dims , n , var .dims ))
140
141
else :
141
- arrays .append (var .values )
142
+ cat = pd .Categorical (var .values , ordered = True )
143
+ labels .append (cat .codes )
144
+ levels .append (cat .categories )
142
145
143
- idx = pd .MultiIndex . from_arrays ( arrays , names = names )
146
+ idx = pd .MultiIndex ( labels = labels , levels = levels , names = names )
144
147
vars_to_replace [dim ] = IndexVariable (dim , idx )
145
148
vars_to_remove .extend (var_names )
146
149
@@ -152,37 +155,44 @@ def merge_indexes(indexers, variables, coord_names, append=False):
152
155
return new_variables , new_coord_names
153
156
154
157
155
- def split_indexes (dim_levels , variables , coord_names , drop = False ):
158
+ def split_indexes (dim , levels , variables , coord_names , drop = False ):
156
159
"""Split multi-indexes into variables.
157
160
158
161
Not public API. Used in Dataset and DataArray reset_index
159
162
methods.
160
163
"""
164
+ if isinstance (dim , basestring ):
165
+ dim = [dim ]
166
+ if levels is not None :
167
+ levels = [levels ]
168
+ if levels is None :
169
+ levels = [None ] * len (dim )
170
+
161
171
vars_to_replace = {}
162
172
vars_to_create = OrderedDict ()
163
173
164
- for dim , levels in dim_levels . items ( ):
165
- current_index = variables [dim ].to_index ()
174
+ for d , levs in zip ( dim , levels ):
175
+ current_index = variables [d ].to_index ()
166
176
if not isinstance (current_index , pd .MultiIndex ):
167
- raise ValueError ("%r has no MultiIndex" % dim )
177
+ raise ValueError ("%r has no MultiIndex" % d )
168
178
169
- if levels is None :
170
- levels = current_index .names
171
- elif not isinstance (levels , (tuple , list )):
172
- levels = [levels ]
179
+ if levs is None :
180
+ levs = current_index .names
181
+ elif not isinstance (levs , (tuple , list )):
182
+ levs = [levs ]
173
183
174
- if len (levels ) == current_index .nlevels :
184
+ if len (levs ) == current_index .nlevels :
175
185
new_index_variable = default_index_coordinate (
176
- dim , current_index .size )
186
+ d , current_index .size )
177
187
else :
178
188
new_index_variable = IndexVariable (
179
- dim , current_index .droplevel (levels ))
180
- vars_to_replace [dim ] = new_index_variable
189
+ d , current_index .droplevel (levs ))
190
+ vars_to_replace [d ] = new_index_variable
181
191
182
192
if not drop :
183
- for lev in levels :
184
- idx = current_index .get_level_values (lev )
185
- vars_to_create [idx .name ] = IndexVariable (dim , idx )
193
+ for level in levs :
194
+ idx = current_index .get_level_values (level )
195
+ vars_to_create [idx .name ] = IndexVariable (d , idx )
186
196
187
197
new_variables = variables .copy ()
188
198
new_variables .update (vars_to_replace )
@@ -1420,26 +1430,22 @@ def swap_dims(self, dims_dict, inplace=False):
1420
1430
return self ._replace_vars_and_dims (variables , coord_names ,
1421
1431
inplace = inplace )
1422
1432
1423
- def set_index (self , indexers = None , append = False , inplace = False ,
1424
- ** kw_indexers ):
1433
+ def set_index (self , append = False , inplace = False , ** indexes ):
1425
1434
"""Set Dataset (multi-)indexes using one or more existing coordinates or
1426
1435
variables.
1427
1436
1428
1437
Parameters
1429
1438
----------
1430
- indexers : dict, optional
1431
- Dictionary with keys given by dimension names and values given by
1432
- (lists of) the names of existing coordinates or variables.
1433
- Any list of multiple names given for a dimension will result as
1434
- a MultiIndex for that dimension.
1435
1439
append : bool, optional
1436
- If True, append the supplied indexers to the existing indexes .
1437
- Otherwise replace the existing indexes (default).
1440
+ If True, append the supplied index(es) to the existing index(es) .
1441
+ Otherwise replace the existing index(es) (default).
1438
1442
inplace : bool, optional
1439
- If True, set new indexes in-place. Otherwise, return a new dataset
1440
- object.
1441
- **kw_indexers : optional
1442
- Keyword arguments in the same form as ``indexers``.
1443
+ If True, set new index(es) in-place. Otherwise, return a new
1444
+ Dataset object.
1445
+ **indexes : {dim: index, ...}
1446
+ Keyword arguments with names matching dimensions and values given
1447
+ by (lists of) the names of existing coordinates or variables to set
1448
+ as new (multi-)index.
1443
1449
1444
1450
Returns
1445
1451
-------
@@ -1450,32 +1456,32 @@ def set_index(self, indexers=None, append=False, inplace=False,
1450
1456
--------
1451
1457
Dataset.reset_index
1452
1458
"""
1453
- indexers = utils .combine_pos_and_kw_args (indexers , kw_indexers ,
1454
- 'set_index' )
1455
- variables , coord_names = merge_indexes (indexers , self ._variables ,
1459
+ variables , coord_names = merge_indexes (indexes , self ._variables ,
1456
1460
self ._coord_names ,
1457
1461
append = append )
1458
1462
return self ._replace_vars_and_dims (variables , coord_names = coord_names ,
1459
1463
inplace = inplace )
1460
1464
1461
- def reset_index (self , dim_levels = None , drop = False , inplace = False ,
1462
- ** kw_dim_levels ):
1463
- """Extract multi-index levels as new coordinates.
1465
+ def reset_index (self , dim , levels = None , drop = False , inplace = False ):
1466
+ """Extract index(es) as new coordinates.
1464
1467
1465
1468
Parameters
1466
1469
----------
1467
- dim_levels : dict, optional
1468
- Dictionary with keys given by dimension names and values given by
1469
- (lists of) the names of the levels to extract, or None to extract
1470
- all levels. Every given dimension must have a multi-index.
1470
+ dim : str or list
1471
+ Name(s) of the dimension(s) for which to extract and reset
1472
+ the index.
1473
+ levels : list or None, optional
1474
+ If None (default) and if `dim` has a multi-index, extract all levels
1475
+ as new coordinates. Otherwise extract only the given list of level
1476
+ names. If more than one dimension is given in `dim`, `levels` should
1477
+ be a list of the same length than `dim` (or simply None to extract
1478
+ all indexes/levels from all given dimensions).
1471
1479
drop : bool, optional
1472
1480
If True, remove the specified levels instead of extracting them as
1473
1481
new coordinates (default: False).
1474
1482
inplace : bool, optional
1475
1483
If True, modify the dataset in-place. Otherwise, return a new
1476
1484
Dataset object.
1477
- **kw_dim_levels : optional
1478
- Keyword arguments in the same form as ``dim_levels``.
1479
1485
1480
1486
Returns
1481
1487
-------
@@ -1486,36 +1492,30 @@ def reset_index(self, dim_levels=None, drop=False, inplace=False,
1486
1492
--------
1487
1493
Dataset.set_index
1488
1494
"""
1489
- dim_levels = utils .combine_pos_and_kw_args (dim_levels , kw_dim_levels ,
1490
- 'reset_index' )
1491
- variables , coord_names = split_indexes (dim_levels , self ._variables ,
1495
+ variables , coord_names = split_indexes (dim , levels , self ._variables ,
1492
1496
self ._coord_names , drop = drop )
1493
1497
return self ._replace_vars_and_dims (variables , coord_names = coord_names ,
1494
1498
inplace = inplace )
1495
1499
1496
- def reorder_levels (self , dim_order = None , inplace = False , ** kw_dim_order ):
1500
+ def reorder_levels (self , inplace = False , ** dim_order ):
1497
1501
"""Rearrange index levels using input order.
1498
1502
1499
1503
Parameters
1500
1504
----------
1501
- dim_order : dict, optional
1502
- Dictionary with keys given by dimension names and values given
1503
- by lists representing new level orders. Every given dimension
1504
- must have a multi-index.
1505
1505
inplace : bool, optional
1506
1506
If True, modify the dataset in-place. Otherwise, return a new
1507
1507
DataArray object.
1508
- **kw_dim_order : optional
1509
- Keyword arguments in the same form as ``dim_order``.
1508
+ **dim_order : optional
1509
+ Keyword arguments with names matching dimensions and values given
1510
+ by lists representing new level orders. Every given dimension
1511
+ must have a multi-index.
1510
1512
1511
1513
Returns
1512
1514
-------
1513
1515
reindexed: Dataset
1514
1516
Another dataset, with this dataset's data but replaced
1515
1517
coordinates.
1516
1518
"""
1517
- dim_order = utils .combine_pos_and_kw_args (dim_order , kw_dim_order ,
1518
- 'reorder_levels' )
1519
1519
replace_variables = {}
1520
1520
for dim , order in dim_order .items ():
1521
1521
coord = self ._variables [dim ]
0 commit comments