@@ -2324,13 +2324,13 @@ def unstack(self, dim):
2324
2324
'a MultiIndex' )
2325
2325
2326
2326
full_idx = pd .MultiIndex .from_product (index .levels , names = index .names )
2327
-
2327
+
2328
2328
# take a shortcut in case the MultiIndex was not modified.
2329
2329
if index .equals (full_idx ):
2330
2330
obj = self
2331
2331
else :
2332
2332
obj = self .reindex (copy = False , ** {dim : full_idx })
2333
-
2333
+
2334
2334
new_dim_names = index .names
2335
2335
new_dim_sizes = [lev .size for lev in index .levels ]
2336
2336
@@ -3360,18 +3360,28 @@ def shift(self, **shifts):
3360
3360
3361
3361
return self ._replace_vars_and_dims (variables )
3362
3362
3363
- def roll (self , ** shifts ):
3363
+ def roll (self , shifts = None , roll_coords = None , ** shifts_kwargs ):
3364
3364
"""Roll this dataset by an offset along one or more dimensions.
3365
3365
3366
- Unlike shift, roll rotates all variables, including coordinates. The
3367
- direction of rotation is consistent with :py:func:`numpy.roll`.
3366
+ Unlike shift, roll may rotate all variables, including coordinates
3367
+ if specified. The direction of rotation is consistent with
3368
+ :py:func:`numpy.roll`.
3368
3369
3369
3370
Parameters
3370
3371
----------
3371
- **shifts : keyword arguments of the form {dim: offset}
3372
- Integer offset to rotate each of the given dimensions. Positive
3373
- offsets roll to the right; negative offsets roll to the left.
3374
3372
3373
+ shifts : dict, optional
3374
+ A dict with keys matching dimensions and values given
3375
+ by integers to rotate each of the given dimensions. Positive
3376
+ offsets roll to the right; negative offsets roll to the left.
3377
+ roll_coords : bool
3378
+ Indicates whether to roll the coordinates by the offset
3379
+ The current default of roll_coords (None, equivalent to True) is
3380
+ deprecated and will change to False in a future version.
3381
+ Explicitly pass roll_coords to silence the warning.
3382
+ **shifts_kwargs : {dim: offset, ...}, optional
3383
+ The keyword arguments form of ``shifts``.
3384
+ One of shifts or shifts_kwargs must be provided.
3375
3385
Returns
3376
3386
-------
3377
3387
rolled : Dataset
@@ -3394,15 +3404,25 @@ def roll(self, **shifts):
3394
3404
Data variables:
3395
3405
foo (x) object 'd' 'e' 'a' 'b' 'c'
3396
3406
"""
3407
+ shifts = either_dict_or_kwargs (shifts , shifts_kwargs , 'roll' )
3397
3408
invalid = [k for k in shifts if k not in self .dims ]
3398
3409
if invalid :
3399
3410
raise ValueError ("dimensions %r do not exist" % invalid )
3400
3411
3412
+ if roll_coords is None :
3413
+ warnings .warn ("roll_coords will be set to False in the future."
3414
+ " Explicitly set roll_coords to silence warning." ,
3415
+ FutureWarning , stacklevel = 2 )
3416
+ roll_coords = True
3417
+
3418
+ unrolled_vars = () if roll_coords else self .coords
3419
+
3401
3420
variables = OrderedDict ()
3402
- for name , var in iteritems (self .variables ):
3403
- var_shifts = dict ((k , v ) for k , v in shifts .items ()
3404
- if k in var .dims )
3405
- variables [name ] = var .roll (** var_shifts )
3421
+ for k , v in iteritems (self .variables ):
3422
+ if k not in unrolled_vars :
3423
+ variables [k ] = v .roll (** shifts )
3424
+ else :
3425
+ variables [k ] = v
3406
3426
3407
3427
return self ._replace_vars_and_dims (variables )
3408
3428
0 commit comments