I need to use the index location of a timeseries in a lambda function. The lambda function needs to use the location of the index in the transformation. Similar to the question raised in this question: Can I use index information inside the map function? but using a pandas dataframe with DateTime as index.
The equation I am looking to get out of the lambda function is:
position of value in index of timeseries x (1/ length of timeseries) + value
The point of this function is to add a linear trend to the timeseries. The output I expect is an increase of +1 by the end of the timeseries relative to the first time step.
My thought so far has been to use a combination of the enumerate and get_loc functions like so:
dates = pd.date_range(start='2018-10-01', end='2019-09-30', freq='D')
df = pd.DataFrame(np.random.randint(0,100,size=(365, 4)), columns=list('ABCD'), index=dates)
a = df['A']
test = map(lambda (idx, val): df.index.get_loc(idx) * (1/len(df.index)) + val, enumerate(a))
I get the following error message:
File "<ipython-input-6-8fb927ed0ecd>", line 8
test = map(lambda (idx, val): df.index.get_loc(idx) * (1/len(df.index)) + val, enumerate(a))
^
SyntaxError: invalid syntax