@@ -548,7 +548,7 @@ def rolling(self, dim=None, min_periods=None, center=False, **dim_kwargs):
548
548
Set the labels at the center of the window.
549
549
**dim_kwargs : optional
550
550
The keyword arguments form of ``dim``.
551
- One of dim or dim_kwarg must be provided.
551
+ One of dim or dim_kwargs must be provided.
552
552
553
553
Returns
554
554
-------
@@ -591,15 +591,17 @@ def rolling(self, dim=None, min_periods=None, center=False, **dim_kwargs):
591
591
return self ._rolling_cls (self , dim , min_periods = min_periods ,
592
592
center = center )
593
593
594
- def resample (self , freq = None , dim = None , how = None , skipna = None ,
595
- closed = None , label = None , base = 0 , keep_attrs = None , ** indexer ):
594
+ def resample (self , indexer = None , skipna = None , closed = None , label = None ,
595
+ base = 0 , keep_attrs = None , ** indexer_kwargs ):
596
596
"""Returns a Resample object for performing resampling operations.
597
597
598
598
Handles both downsampling and upsampling. If any intervals contain no
599
599
values from the original object, they will be given the value ``NaN``.
600
600
601
601
Parameters
602
602
----------
603
+ indexer : {dim: freq}, optional
604
+ Mapping from the dimension name to resample frequency.
603
605
skipna : bool, optional
604
606
Whether to skip missing values when aggregating in downsampling.
605
607
closed : 'left' or 'right', optional
@@ -614,9 +616,9 @@ def resample(self, freq=None, dim=None, how=None, skipna=None,
614
616
If True, the object's attributes (`attrs`) will be copied from
615
617
the original object to the new one. If False (default), the new
616
618
object will be returned without attributes.
617
- **indexer : {dim: freq}
618
- Dictionary with a key indicating the dimension name to resample
619
- over and a value corresponding to the resampling frequency .
619
+ **indexer_kwargs : {dim: freq}
620
+ The keyword arguments form of ``indexer``.
621
+ One of indexer or indexer_kwargs must be provided .
620
622
621
623
Returns
622
624
-------
@@ -664,30 +666,24 @@ def resample(self, freq=None, dim=None, how=None, skipna=None,
664
666
if keep_attrs is None :
665
667
keep_attrs = _get_keep_attrs (default = False )
666
668
667
- if dim is not None :
668
- if how is None :
669
- how = 'mean'
670
- return self ._resample_immediately (freq , dim , how , skipna , closed ,
671
- label , base , keep_attrs )
669
+ # note: the second argument (now 'skipna') use to be 'dim'
670
+ if ((skipna is not None and not isinstance (skipna , bool ))
671
+ or ('how' in indexer_kwargs and 'how' not in self .dims )
672
+ or ('dim' in indexer_kwargs and 'dim' not in self .dims )):
673
+ raise TypeError ('resample() no longer supports the `how` or '
674
+ '`dim` arguments. Instead call methods on resample '
675
+ "objects, e.g., data.resample(time='1D').mean()" )
676
+
677
+ indexer = either_dict_or_kwargs (indexer , indexer_kwargs , 'resample' )
672
678
673
- if (how is not None ) and indexer :
674
- raise TypeError ("If passing an 'indexer' then 'dim' "
675
- "and 'how' should not be used" )
676
-
677
- # More than one indexer is ambiguous, but we do in fact need one if
678
- # "dim" was not provided, until the old API is fully deprecated
679
679
if len (indexer ) != 1 :
680
680
raise ValueError (
681
681
"Resampling only supported along single dimensions."
682
682
)
683
683
dim , freq = indexer .popitem ()
684
684
685
- if isinstance (dim , basestring ):
686
- dim_name = dim
687
- dim = self [dim ]
688
- else :
689
- raise TypeError ("Dimension name should be a string; "
690
- "was passed %r" % dim )
685
+ dim_name = dim
686
+ dim_coord = self [dim ]
691
687
692
688
if isinstance (self .indexes [dim_name ], CFTimeIndex ):
693
689
raise NotImplementedError (
@@ -702,63 +698,15 @@ def resample(self, freq=None, dim=None, how=None, skipna=None,
702
698
'errors.'
703
699
)
704
700
705
- group = DataArray (dim , [(dim .dims , dim )], name = RESAMPLE_DIM )
701
+ group = DataArray (dim_coord , coords = dim_coord .coords ,
702
+ dims = dim_coord .dims , name = RESAMPLE_DIM )
706
703
grouper = pd .Grouper (freq = freq , closed = closed , label = label , base = base )
707
704
resampler = self ._resample_cls (self , group = group , dim = dim_name ,
708
705
grouper = grouper ,
709
706
resample_dim = RESAMPLE_DIM )
710
707
711
708
return resampler
712
709
713
- def _resample_immediately (self , freq , dim , how , skipna ,
714
- closed , label , base , keep_attrs ):
715
- """Implement the original version of .resample() which immediately
716
- executes the desired resampling operation. """
717
- from .dataarray import DataArray
718
- from ..coding .cftimeindex import CFTimeIndex
719
-
720
- RESAMPLE_DIM = '__resample_dim__'
721
-
722
- warnings .warn ("\n .resample() has been modified to defer "
723
- "calculations. Instead of passing 'dim' and "
724
- "how=\" {how}\" , instead consider using "
725
- ".resample({dim}=\" {freq}\" ).{how}('{dim}') " .format (
726
- dim = dim , freq = freq , how = how ),
727
- FutureWarning , stacklevel = 3 )
728
-
729
- if isinstance (self .indexes [dim ], CFTimeIndex ):
730
- raise NotImplementedError (
731
- 'Resample is currently not supported along a dimension '
732
- 'indexed by a CFTimeIndex. For certain kinds of downsampling '
733
- 'it may be possible to work around this by converting your '
734
- 'time index to a DatetimeIndex using '
735
- 'CFTimeIndex.to_datetimeindex. Use caution when doing this '
736
- 'however, because switching to a DatetimeIndex from a '
737
- 'CFTimeIndex with a non-standard calendar entails a change '
738
- 'in the calendar type, which could lead to subtle and silent '
739
- 'errors.'
740
- )
741
-
742
- if isinstance (dim , basestring ):
743
- dim = self [dim ]
744
-
745
- group = DataArray (dim , [(dim .dims , dim )], name = RESAMPLE_DIM )
746
- grouper = pd .Grouper (freq = freq , how = how , closed = closed , label = label ,
747
- base = base )
748
- gb = self ._groupby_cls (self , group , grouper = grouper )
749
- if isinstance (how , basestring ):
750
- f = getattr (gb , how )
751
- if how in ['first' , 'last' ]:
752
- result = f (skipna = skipna , keep_attrs = keep_attrs )
753
- elif how == 'count' :
754
- result = f (dim = dim .name , keep_attrs = keep_attrs )
755
- else :
756
- result = f (dim = dim .name , skipna = skipna , keep_attrs = keep_attrs )
757
- else :
758
- result = gb .reduce (how , dim = dim .name , keep_attrs = keep_attrs )
759
- result = result .rename ({RESAMPLE_DIM : dim .name })
760
- return result
761
-
762
710
def where (self , cond , other = dtypes .NA , drop = False ):
763
711
"""Filter elements from this object according to a condition.
764
712
0 commit comments