0

I have a pandas Series object with dates as index and values as a share price of a company. I would like to slice the data, so that I have let´s say a date 10.01.2022, and I want a slice from 3 previous dates and 5 next days from this date. Is that easily done? Or do I have to convert it, add/subtract those numbers from that date, and convert back? I´m a bit lost in all that datetime, strptime, to_datetime,...

Something like this:

date = "10.01.2022"

share_price = [date - 3 : date + 5]

Thank you

1 Answers1

1

You can use .loc[]. Both ends will be inclusive.

Example:

s = pd.Series([1,2,3,4,5,6],
              index = pd.to_datetime([
                '07.01.2022', '09.01.2022', '10.01.2022',
                '12.01.2022', '15.01.2022', '16.01.2022'
              ], dayfirst=True))

date = pd.to_datetime("10.01.2022", dayfirst=True)

s:

2022-01-07    1
2022-01-09    2
2022-01-10    3
2022-01-12    4
2022-01-15    5
2022-01-16    6
dtype: int64

date:

Timestamp('2022-01-10 00:00:00')
s.loc[date - pd.Timedelta('3d') : date + pd.Timedelta('5d')]


2022-01-07    1
2022-01-09    2
2022-01-10    3
2022-01-12    4
2022-01-15    5
dtype: int64

Edit:

To add business days:

from pandas.tseries.offsets import BDay

s.loc[date - BDay(3) : date + BDay(5)]
Vladimir Fokow
  • 3,728
  • 2
  • 5
  • 27
  • Thank you very much for your suggestion. But that doesn´t account for weekends/holidays. So when I add +5 days, it returns me only like 3 days. Do you know how to solve this please? I tried to look up business days, something like "5b" instead of "5d", but it looks like it´s not supported. – Vladimír Ondrášek Aug 25 '22 at 18:04
  • @VladimírOndrášek, updated my answer if you want to [add business days](https://stackoverflow.com/q/31588148/14627505). – Vladimir Fokow Aug 25 '22 at 18:14
  • In my example the entries and business days are the same. To understand my intentions, I downloaded a csv file from yahoo finance with share prices of a company throughout time and I want to do an event study, where I need to calculate an event window of let´s say 15 days prior to event day and 5 days after. So when you download this csv, the dates are business/trading days. – Vladimír Ondrášek Aug 25 '22 at 18:24
  • @VladimírOndrášek, good. Is my new solution working? – Vladimir Fokow Aug 25 '22 at 18:26
  • Perfect, thank you very much :) works as intended. – Vladimír Ondrášek Aug 25 '22 at 18:57
  • Vladimir, do you happen to know, how to alter the code, so that it add and deducts actual rows, not business days? Because when I tried the code with the real numbers I wanted to use to add/deduct, it does it wrong, it was shifted of one day. I suppose because some holiday might be counted as a business day. Maybe to convert it into integers and then convert it back or something... – Vladimír Ondrášek Sep 03 '22 at 20:39
  • Yes, there is an easy way! See [`.iloc[]`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.iloc.html). If you want, you can also [combine positional and label-based indexing](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#combining-positional-and-label-based-indexing) – Vladimir Fokow Sep 04 '22 at 08:50
  • Would you be so kind a provide a code for this? I basically know how .iloc[] works, I read the material you sent, but cannot think of how to do it, because I don´t know the exact position/number of the index I want to add/subtract. It should be a relative number from that one specified date, if you understand me. – Vladimír Ondrášek Sep 04 '22 at 20:59
  • would [`.get_loc`](https://pandas.pydata.org/docs/reference/api/pandas.Index.get_loc.html) help? https://stackoverflow.com/a/34897532/14627505 – Vladimir Fokow Sep 05 '22 at 08:33