I have a pandas df of daily stock prices that contains data from 2007 to the present. The df has a multi-index of Ticker and Date. I am trying to get a new df that is a subset of this data that only has data from 2021 onwards.
This code works, but is very slow.
# reduce the dataset to just data since start date
start_date = pd.to_datetime("2021-01-01")
new_df = df.iloc[df.index.get_level_values('Date') >= start_date]
I tried another way, but it only returns values for the exact date. How do I change it to select a range from 2021 onwards?
new_df = df.loc[(slice(None), '2021-01-01'), :]
Another solution would be to reset the index to get the date into a column, and then filter and reindex, but I thought there must be an easier way to do this.