I'm trying to select certain rows in a dataframe based on a list. If you set up the index of a dataframe as a DatetimeIndex you can select just by:
example_df['2018-12-12']
But you can't select multiple dates like this:
example_df[['2018-12-12', '2018-12-05']]
I know I can do the following, but I don't want to type the whole list in case it was longer:
example_df['2018-12-12'] & example_df['2018-12-05'] & ...
Also I know I can use the isin()
method but I want to take advantage of the native date selector in pandas because I belive is faster.
Here is the code:
genesis_block_date = pd.to_datetime('01/03/2009 18:15:05 GMT')
end_date = pd.to_datetime('01/03/2029')
# Halving dates
halving_dates = ['November 28, 2012', 'July 9th, 2016', '14 May, 2020']
halving_dates = pd.to_datetime(halving_dates)
approx_block_gen_time = pd.to_timedelta('10m')
date_range = pd.date_range(start=genesis_block_date, end=end_date, freq=approx_block_gen_time)
columns = ['days_until_halving']
df_new_features = pd.DataFrame(index=date_range, columns=columns)
df_new_features[halving_dates] = ...