I'm trying to deploy a custom function using apply on a resampled object. The tricky part in the function is that it loops across each timestamp of the passed dataframe and performs operations based on values of other columns for that timestamp. Then it would output a dataframe of the same rowcount as in the input one (which in my toy example I'm not doing, just returning a list). The logic in the example I provide is much simpler than in my use-case.
Getting an IndexingError: Too many indexers
import numpy as np
import pandas as pd
df = pd.DataFrame({'a': np.random.randint(0, 100, 10), 'b': np.random.randint(0, 1000, 10), 'c': np.random.uniform(0, 100, 10)},
index = pd.date_range("2021-01-01", "2021-01-10"))
def test_func(df):
new_ser = []
for i in range(df.shape[0]):
if i==0:
new_ser.append(np.NaN)
if df.iloc[i,:]['a'] < df.iloc[i,:]['b']:
new_ser.append(1)
else:
new_ser.append(0)
return new_ser
df.resample('2D').apply(test_func)
IndexingError: Too many indexers