How to filter only specific intervals of time in time series using python

Viewed 67

I am trying to study specific intervals of time series, but I don't know to filter only those intervals starting from the month sepetember to March. and that will be repeated for all the years.

The data that I have :

Date        A   B
01/01/2017  41  10.7
02/01/2017  567 5.55
03/01/2017  89  6.23
... ... ...
01/12/2020  78  10.74
02/12/2020  29  10.44
... ... ...

Thank you.

1 Answers

Here I have requested financial data from pandas_datareader and filtered it using masking. Note that the date column must be an index. You can set a Date column as an index like this:

df['Date'] = pd.to_datetime(df['Date'])#if the column does not have a date format, then convert it.
df = df.set_index('Date')

In pandas_datareader, the date column is an index.

import pandas_datareader.data as web
import pandas as pd

df = web.DataReader('GE', 'yahoo', start='2017-01-10', end='2019-10-09')
df1 = df[(df.index.month >= 9) | (df.index.month <= 3)]
print(df1)
Related