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

Time:06-16

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

Here is the data I have :

Year    A          B    C        Date
1990    0.00    5.37    4.63    01/01/1990
1990    0.00    5.55    2.73    02/01/1990
1990    0.00    6.23    2.33    03/01/1990
... ... ... ... ...
2021    0.08    10.74   2.56    27/12/2021
2021    0.06    10.44   3.92    28/12/2021
2021    0.02    9.22    4.04    29/12/2021
2021    0.00    7.26    3.20    30/12/2021
2021    0.00    7.08    3.55    31/12/2021

The data that I want :

 Year interval  A       B       C        Date
    1990-1991   0.00    5.37    4.63    01/09/1990
    1990-1991   0.00    5.55    2.73    02/09/1990
    1990-1991   0.00    6.23    2.33    03/09/1990
    …   ... ... ... ...
    1990-1991   0.08    10.74   2.56    01/01/1991
    1990-1991   0.06    10.44   3.92    02/01/1991
    …   ... ... ... ...
    1990-1991   0.02    9.22    4.04    31/03/1991
    1991-1992   0.00    7.26    3.20    01/09/1991
    1991-1992   0.00    7.08    3.55    02/09/1991
    1991-1992   0.01    7.09    3.56    03/09/1991
    …   ... ... …   ...
    1991-1992   0.08    10.74   2.56    01/01/1992
    1991-1992   0.06    10.44   3.92    02/01/1992
    …   ... ... ... ...
    1991-1992   0.02    9.22    4.04    31/03/1992
    1992-1993   0.03    9.23    4.05    01/09/1992

I already check in many ximilar quetions but i did not find how to do this specific intervals.

I hope you help me

Thank you

CodePudding user response:

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