@@ -133,7 +133,7 @@ def quantile(series, quantile=None):
133
133
134
134
135
135
def resample_apply (rule : str ,
136
- func : Callable ,
136
+ func : Optional [ Callable [..., Sequence ]] ,
137
137
series ,
138
138
* args ,
139
139
agg = 'last' ,
@@ -205,6 +205,10 @@ def SMA(series, n):
205
205
self.sma = self.I(SMA, daily, 10, plot=False)
206
206
207
207
"""
208
+ if func is None :
209
+ def func (x , * _ , ** __ ):
210
+ return x
211
+
208
212
if not isinstance (series , (pd .Series , pd .DataFrame )):
209
213
assert isinstance (series , _Array ), \
210
214
'resample_apply() takes either a `pd.Series`, `pd.DataFrame`, ' \
@@ -228,14 +232,20 @@ def SMA(series, n):
228
232
def strategy_I (func , * args , ** kwargs ):
229
233
return func (* args , ** kwargs )
230
234
231
- # Resample back to data index
232
235
def wrap_func (resampled , * args , ** kwargs ):
233
- ind = pd .Series (np .asarray (func (resampled , * args , ** kwargs )),
234
- index = resampled .index ,
235
- name = resampled .name )
236
- ind = ind .reindex (index = series .index | resampled .index ,
237
- method = 'ffill' ).reindex (series .index )
238
- return ind
236
+ result = func (resampled , * args , ** kwargs )
237
+ if not isinstance (result , pd .DataFrame ) and not isinstance (result , pd .Series ):
238
+ result = np .asarray (result )
239
+ if result .ndim == 1 :
240
+ result = pd .Series (result , name = resampled .name )
241
+ elif result .ndim == 2 :
242
+ result = pd .DataFrame (result .T )
243
+ # Resample back to data index
244
+ if not result .index .is_all_dates :
245
+ result .index = resampled .index
246
+ result = result .reindex (index = series .index | resampled .index ,
247
+ method = 'ffill' ).reindex (series .index )
248
+ return result
239
249
240
250
wrap_func .__name__ = func .__name__
241
251
0 commit comments