July 2019
Beginner to intermediate
740 pages
16h 52m
English
We can use shift() to create lagged data. By default, the shift will be by one period, but this can be any integer (positive or negative). Let's use shift() to create a new column that indicates the previous day's closing price for the daily Facebook stock data. From this new column, we can calculate the price change due to after-hours trading (after the market close one day right up to the market open the following day):
>>> fb.assign(... prior_close=lambda x: x.close.shift(),... after_hours_change_in_price=lambda x: x.open - x.prior_close,... abs_change=lambda x: x.after_hours_change_in_price.abs()... ).nlargest(5, 'abs_change')
This gives us the days that were most affected by after-hours trading:
| open | high ... |
|---|
Read now
Unlock full access