I'm trying to combine and average multiple (10-100 per call) data series, with each data series of approx shape=(1,100). I want to average the values of each result and output a series of equal length, ie output[i] = mean(series0[i], series1[i], series2[i].... This will be called ~10k times a day in early production, hopefully much more later, so I'm interested in wider tips or references if possible.
Currently the extant code in development is heavy on pandas for it's easy readability but is easily amended to output pandas.Series, python3 lists, or numpy.arrays, so anything goes. At a guess, I imagine some or all pandas will eventually be cut in favour of numpy.arrays and lists/dicts for speed/memory/cost reasons. I know enough to write the below code and know just about enough that a list comprehension may be a good contender, but I'm very much learning as I go so please be gentle.
I could find posts on merge/concat speeds, but rarely is this combined with further functions. So... suggestions on faster ways to produce an average series?
import numpy as np
series_length = 100
repeats=10
def foo(length):
return np.random.randint(0,500,length,int)
results = []
for i in range(repeats):
results.append(foo(series_length)) # produce a list n long, each containing a len=100 series/array/list (format optional) of integers
def some_code_here(data):
avg_results = [np.mean([series[i] for series in data]) for i in range(series_length)]
return avg_results
# Output length = series_length
final_solution = some_code_here(results)