0

I want to create a table of stocks consisting of three columns:

Quantity Day - 1 | Quantity traded | Quantity Day 0

The stocks come in a frame like :

> df
Date        Stock     Quantity
2019-04-01  ALSC3      19600
            AMAR3      3080
2019-04-02  ALSC3       4000
            AMAR3      3070

I expected that a simple shift would match the index (Date,StockTicker), but in reality it ignores the index and shifts the row entirely.

Date        Stock     Quantity Day-1
2019-04-01  ALSC3      3080
            AMAR3      4000
2019-04-02  ALSC3      3070 
            AMAR3      NaN

Does anyone know how to perform it correctly? I would like something like this:

Date        Stock     Quantity     Quantity Day -1
2019-04-01  ALSC3      19600       4000
            AMAR3      3080        3070
2019-04-02  ALSC3      4000        NaN
            AMAR3      3070        NaN

1 Answer 1

2

Here you go, assuming the date index are countinuous.

df.Quantity.groupby(level=1).shift(-1)

This matches your expected output. Although I think "Quantity Day-1" means shift() instead of shift(-1).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your quick response! I used for my time series and it worked perfectly, even if if the dates that skip weekends.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.