What I am trying to achieve is this: I have several Timeseries, which I need to combine on a per-point-basis and return the result as a single new timeseries.
I understand that you can use various numpy
functions on Series in pandas
, but I am unclear on how to apply complex functions to several timeseries.
The Function I want to apply:
def direction_day(y_values):
# taking a numpy array of floats
sig_sum = np.sum(np.sign(y_values))
abs_sum = np.sum(np.abs(np.sign(y_values)))
return (sig_sum / abs_sum)
Example of my current TimeSeries
Objects:
def ret_random_ts():
dates = ['2016-1-{}'.format(i)for i in range(1,21)]
values = [np.random.randn(4,3) for i in range(20)]
return pd.Series(values, index=dates)
Of course I can always just loop through the TimeSeries
with for
loops and glue them together.
I was wondering, however, if there was an option to pass a function to a TimeSeries
object containing multiple values per date, and apply that function for each date?
I.e.:
ts = ret_random_ts()
ts.apply_func(direction_day,Series['Dates'])