1

I have data on the stock market for the past 20 years. There are roughly 10 million entries. Each entry is the stock symbol, the Date, and the close price of the stock for that day. Lets say I have the stock A on Jan 1 1999. I am trying to add a column to the existing data frame that contains the close price of stock A on Jan 1 2000. However there is an issue because Jan 1 2000 is a weekend and there is no stock data for this day. The code I have instead takes the data from 2001 and pastes it into the added column. Ideally, instead of taking 2001's data, I would like the new column for the entry "A" on Jan 1 1999 to just say 'n/a'. I am not sure how to do this

The current code I have uses the shift function and the code is as follows:

df['Following Year Close Price'] = df.groupby(['Symbol'], df['Date'].dt.month, df[Date'].dt.day])['Close'].shify(-1)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • Welcome to Stack Overflow! Your question/problem is not very clear. – Jeroen Heier Aug 11 '19 at 04:22
  • [Provide a copy of the data](https://stackoverflow.com/questions/52413246/how-do-i-provide-a-reproducible-copy-of-my-existing-dataframe) and it's helpful to provide an example of the expected output. – Trenton McKinney Aug 11 '19 at 04:48
  • 1
    Hello, I am wondering why you actually need to add an explicit "n/a" to your data for every calendar date. The Pandas developers have spent a lot of time thinking about the issues that arise from missing data and calendar time series. If you follow their conventions, you may not need to add data. Please explain your reasons for doing this. – John Ladasky Aug 11 '19 at 05:28
  • I do not need an explicit n/a to my data. I just dont want the data added to the new column to skip over years and be incorrect. If the data that needs to be added to the new column does not exist, I figured putting 'n/a' would make the most sense, but I am open to other ideas on how to address the issue on missing data for the calendar time series. Thank you I appreciate the help – coderboy2240 Aug 11 '19 at 15:21

0 Answers0