Pandas DF stacked filters not working, getting error on range

Viewed 39

I'm trying to filter based on both date range and columns, then present graphs of the data. Unfortunately I'm getting an error when I do.

import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plot

ticker = yf.Ticker("mnf.ax")
history = ticker.history(period="max").reset_index()
history["Date"] = pd.to_datetime(history["Date"])
history = history.set_index('Date')

# this works
history['2019-01-01' : '2020-07-01'].resample('W').mean().plot.line(figsize=(20, 16), subplots=True)

# This also works
history[['Low','High']].resample('W').mean().plot.line(figsize=(20, 16), subplots=True)

# However this doesn't
history[['2019-01-01' : '2020-07-01'],['Low','High]]'.resample('W').mean().plot.line(figsize=(20, 16), subplots=True)

Would love some insight

1 Answers

You seem to be incorrectly using the brackets. I suggest you use loc for this case:

history.loc['2019-01-01':'2020-07-01'][['Low','High']].resample('W').mean().plot.line(figsize=(20, 16), subplots=True)

Disaggregating:

history.loc['2019-01-01':'2020-07-01'] #Returns the full dataframe within the specified dates

[['Low','High']] # Selects the specific columns

.resample('W').mean().plot.line(figsize=(20, 16), subplots=True) ## Resamples, aggregates using mean and plots.
Related