'What is the best way to select rows within a date range from a timeseries dataset in a pandas dataframe?

I'm working with CRYPTO_INTRADAY timeseries data from the AlphaVantage API. JSON format:

{'Meta Data': {'1. Information': 'Crypto Intraday (15min) Time Series', '2. Digital Currency Code': 'ZEC', '3. Digital Currency Name': 'Zcash', '4. Market Code': 'USD', '5. Market Name': 'United States Dollar', '6. Last Refreshed': '2022-02-15 18:15:00', '7. Interval': '15min', '8. Output Size': 'Full size', '9. Time Zone': 'UTC'}, 'Time Series Crypto (15min)': {'2022-02-15 18:15:00': {'1. open': '125.60000', '2. high': '126.00000', '3. low': '125.60000', '4. close': '125.90000', '5. volume': 711}, '2022-02-15 18:00:00': {'1. open': '125.70000', '2. high': '125.90000', '3. low': '125.50000', '4. close': '125.60000', '5. volume': 1059}, '2022-02-15 17:45:00': {'1. open': '125.80000', '2. high': '125.90000', '3. low': '125.50000', '4. close': '125.70000', '5. volume': 857}, '2022-02-15 17:30:00': {'1. open': '125.30000', '2. high': '126.20000', '3. low': '125.30000', '4. close': '125.70000', '5. volume': 1106}, '2022-02-15 17:15:00': {'1. open': '125.90000', '2. high': '126.10000', '3. low': '125.30000', '4. close': '125.30000', '5. vol

Reviewing the various functions for selecting a date range in a dataframe, it seems pandas.DataFrame.between_time is the best option.

However, that function returns an error message:

TypeError: Index must be DatetimeIndex

In trying to make my Index into DatetimeIndex, I'm using the following:

print (df.index.name)

Which returns:

timeStamp

And then run:

df["timeStamp"]=pd.to_datetime(df["timeStamp"])

But that gives a long error message ending with:

KeyError: 'timeStamp'

So I'm wondering if I'm going about creating a DatetimeIndex correctly, or if pandas.DataFrame.between_time is even the right way to go about selecting for a date range within a DataFrame.

Thank you for any guidance on this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source