0

I have a set of indices that match a condition:

for col in signal.columns:
    for sign in signs:
        index = signal.index[signal[col] == sign].tolist()
        for i in index:

at this point, I would like to look at the range of values for this column at i to i + n days in advance (where n is an integer); something like:

            print(signal[col].iloc[i:i + n])

however, I cannot add the int to the date i:

TypeError: unsupported operand type(s) for +: 'datetime.date' and 'int'
roberto tomás
  • 4,435
  • 5
  • 42
  • 71

2 Answers2

1

You will have to use dateutil.relativedelta lib to compute your dates and uisng .loc[date] you will be able to locate your row data.

vbn
  • 274
  • 2
  • 7
1

iloc() looks primarily at one row (the 'i' part), where .loc() will give you a slice. See iloc() docs here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html

You should just be able to use your code with .loc instead of iloc, or just a slice. But notice the slight differences in what is returned. My example has a column named 'Open'.

i=10
n=5
col = 'Open'


In [38]: print(df[col].loc[i])
1.1778571

In [39]: print(df[col].loc[i:i+n])
10   1.178
11   1.135
12   1.156
13   1.154
14   1.104
15   1.082
Name: Open, dtype: float64

In [40]: print(df[col][i:i+n]) #one less row than inlcuding .loc()
10   1.178
11   1.135
12   1.156
13   1.154
14   1.104
Name: Open, dtype: float64
Jonathan Leon
  • 5,440
  • 2
  • 6
  • 14