0

I have two dataframes. One contains a column that contains the date of earnings for a stock. The other contains the all the prices for the stock, keep in mind that the index is the date. I want to get the prices of a stock N days before and after earnings and store it in a new dataframe column wise. This is what I have so far

earningsPrices = pd.DataFrame()
for date in dates:
    earningsPrices[date] = prices[date - pd.Timedelta(days=N):date + pd.Timedelta(days=N)]
print(earningsPrices)

and this is the output enter image description here The problem is that it only writes the prices for the first date, and not the rest.

Tomward Matthias
  • 127
  • 1
  • 13
  • @wjandrea can you elaborate on what is missing? I have said what is contained in the dataframes? Pasting a link, does not help anyone, thanks! – Tomward Matthias Jan 15 '23 at 23:12
  • I'm not sure why my first comment was removed; that's weird... Anyway, you haven't provided any input data, so we can't verify the issue without making up our own data, which is tedious, and might not even reproduce the problem, hypothetically. Also, I forgot to mention, [please don't post pictures of text](https://meta.stackoverflow.com/q/285551/4518341); post the text itself. Here are the resources I mentioned previously: [mre], [How to make good reproducible pandas examples](/q/20109391/4518341). It'd also help to provide your expected output, for completeness. – wjandrea Jan 15 '23 at 23:33
  • It's unclear what your two dataframes look like and what your expected output is. Please provide sample data, i.e., a dataframe with a few example records to show the structure. Why does your screenshot show a dataframe of datetimes in index and columns, while I would have inferred, based on your problem description, that you want a new dataframe with columns: `stock`, `date`, `earnings`, `stock_price_Ndbefore`, `stock_price_Ndafter`? – Harry Haller Jan 16 '23 at 06:56

2 Answers2

0

You should maybe take this approach:

earningsPrices = pd.DataFrame(index=dates, columns=['price1', 'price2', 'price3'])

for date in dates:
    start_date = date - pd.Timedelta(days=N)
    end_date = date + pd.Timedelta(days=N)

    selected_rows = prices.loc[prices['date_column'].between(start_date, end_date)]

    earningsPrices.loc[date, 'price1'] = selected_rows['price1'].values
    earningsPrices.loc[date, 'price2'] = selected_rows['price2'].values
    earningsPrices.loc[date, 'price3'] = selected_rows['price3'].values

print(earningsPrices)
0

use concat

for date in dates:
    earningsPeriod = prices[date - pd.Timedelta(days=window):date + pd.Timedelta(days=window)].reset_index(drop=True)
    earningsPrices = pd.concat([earningsPrices, earningsPeriod], axis=1)
Tomward Matthias
  • 127
  • 1
  • 13