13
13
from pandas .core .indexes .datetimes import DatetimeIndex , date_range
14
14
from pandas .core .indexes .timedeltas import TimedeltaIndex
15
15
from pandas .tseries .offsets import DateOffset , Tick , Day , _delta_to_nanoseconds
16
- from pandas .core .indexes .period import PeriodIndex , period_range
16
+ from pandas .core .indexes .period import PeriodIndex
17
17
import pandas .core .common as com
18
18
import pandas .core .algorithms as algos
19
19
@@ -793,6 +793,11 @@ class PeriodIndexResampler(DatetimeIndexResampler):
793
793
def _resampler_for_grouping (self ):
794
794
return PeriodIndexResamplerGroupby
795
795
796
+ def _get_binner_for_time (self ):
797
+ if self .kind == 'timestamp' :
798
+ return super (PeriodIndexResampler , self )._get_binner_for_time ()
799
+ return self .groupby ._get_period_bins (self .ax )
800
+
796
801
def _convert_obj (self , obj ):
797
802
obj = super (PeriodIndexResampler , self )._convert_obj (obj )
798
803
@@ -817,29 +822,6 @@ def _convert_obj(self, obj):
817
822
818
823
return obj
819
824
820
- def aggregate (self , arg , * args , ** kwargs ):
821
- result , how = self ._aggregate (arg , * args , ** kwargs )
822
- if result is None :
823
- result = self ._downsample (arg , * args , ** kwargs )
824
-
825
- result = self ._apply_loffset (result )
826
- return result
827
-
828
- agg = aggregate
829
-
830
- def _get_new_index (self ):
831
- """ return our new index """
832
- ax = self .ax
833
-
834
- if len (ax ) == 0 :
835
- values = []
836
- else :
837
- start = ax [0 ].asfreq (self .freq , how = self .convention )
838
- end = ax [- 1 ].asfreq (self .freq , how = 'end' )
839
- values = period_range (start , end , freq = self .freq ).asi8
840
-
841
- return ax ._shallow_copy (values , freq = self .freq )
842
-
843
825
def _downsample (self , how , ** kwargs ):
844
826
"""
845
827
Downsample the cython defined function
@@ -857,21 +839,9 @@ def _downsample(self, how, **kwargs):
857
839
how = self ._is_cython_func (how ) or how
858
840
ax = self .ax
859
841
860
- new_index = self ._get_new_index ()
861
-
862
- # Start vs. end of period
863
- memb = ax .asfreq (self .freq , how = self .convention )
864
-
865
842
if is_subperiod (ax .freq , self .freq ):
866
843
# Downsampling
867
- if len (new_index ) == 0 :
868
- bins = []
869
- else :
870
- i8 = memb .asi8
871
- rng = np .arange (i8 [0 ], i8 [- 1 ] + 1 )
872
- bins = memb .searchsorted (rng , side = 'right' )
873
- grouper = BinGrouper (bins , new_index )
874
- return self ._groupby_and_aggregate (how , grouper = grouper )
844
+ return self ._groupby_and_aggregate (how , grouper = self .grouper )
875
845
elif is_superperiod (ax .freq , self .freq ):
876
846
return self .asfreq ()
877
847
elif ax .freq == self .freq :
@@ -905,9 +875,10 @@ def _upsample(self, method, limit=None, fill_value=None):
905
875
return super (PeriodIndexResampler , self )._upsample (
906
876
method , limit = limit , fill_value = fill_value )
907
877
878
+ self ._set_binner ()
908
879
ax = self .ax
909
880
obj = self .obj
910
- new_index = self ._get_new_index ()
881
+ new_index = self .binner
911
882
912
883
# Start vs. end of period
913
884
memb = ax .asfreq (self .freq , how = self .convention )
@@ -1269,6 +1240,29 @@ def _get_time_period_bins(self, ax):
1269
1240
1270
1241
return binner , bins , labels
1271
1242
1243
+ def _get_period_bins (self , ax ):
1244
+ if not isinstance (ax , PeriodIndex ):
1245
+ raise TypeError ('axis must be a PeriodIndex, but got '
1246
+ 'an instance of %r' % type (ax ).__name__ )
1247
+
1248
+ if not len (ax ):
1249
+ binner = labels = PeriodIndex (
1250
+ data = [], freq = self .freq , name = ax .name )
1251
+ return binner , [], labels
1252
+
1253
+ start = ax [0 ].asfreq (self .freq , how = self .convention )
1254
+ end = ax [- 1 ].asfreq (self .freq , how = 'end' )
1255
+
1256
+ labels = binner = PeriodIndex (start = start , end = end ,
1257
+ freq = self .freq , name = ax .name )
1258
+
1259
+ memb = ax .asfreq (self .freq , how = self .convention )
1260
+ i8 = memb .asi8
1261
+ rng = np .arange (i8 [0 ], i8 [- 1 ] + 1 )
1262
+ bins = memb .searchsorted (rng , side = 'right' )
1263
+
1264
+ return binner , bins , labels
1265
+
1272
1266
1273
1267
def _take_new_index (obj , indexer , new_index , axis = 0 ):
1274
1268
from pandas .core .api import Series , DataFrame
0 commit comments