Home > Net >  Python - Applying values for specific months across entire DateTime Index
Python - Applying values for specific months across entire DateTime Index

Time:12-13

I want to be able to make ["Indicator"] Col = True for the first 6 months of every year across the entire Data Frame. I need it to be variable as well. So I can later apply to the first 3 months or first 10 etc. for each year.

Added copy/paste code for dict to df

Setup df:

import pandas as pd
from pandas import Timestamp
nan = 0

data = {'Price': {Timestamp('2018-01-01 00:00:00', freq='MS'): 100,
  Timestamp('2018-02-01 00:00:00', freq='MS'): 100,
  Timestamp('2018-03-01 00:00:00', freq='MS'): 105,
  Timestamp('2018-04-01 00:00:00', freq='MS'): 105,
  Timestamp('2018-05-01 00:00:00', freq='MS'): 105,
  Timestamp('2018-06-01 00:00:00', freq='MS'): 108,
  Timestamp('2018-07-01 00:00:00', freq='MS'): 108,
  Timestamp('2018-08-01 00:00:00', freq='MS'): 108,
  Timestamp('2018-09-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-10-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-11-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-12-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-01-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-02-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-03-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-04-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-05-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-06-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-07-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-08-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-09-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-10-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-11-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-12-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-01-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-02-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-03-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-04-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-05-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-06-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-07-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-08-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-09-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-10-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-11-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-12-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-01-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-02-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-03-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-04-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-05-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-06-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-07-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-08-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-09-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-10-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-11-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-12-01 00:00:00', freq='MS'): nan,
  Timestamp('2022-01-01 00:00:00', freq='MS'): 210,
  Timestamp('2022-02-01 00:00:00', freq='MS'): 200,
  Timestamp('2022-03-01 00:00:00', freq='MS'): 261,
  Timestamp('2022-04-01 00:00:00', freq='MS'): 220,
  Timestamp('2022-05-01 00:00:00', freq='MS'): 200,
  Timestamp('2022-06-01 00:00:00', freq='MS'): 180,
  Timestamp('2022-07-01 00:00:00', freq='MS'): 185,
  Timestamp('2022-08-01 00:00:00', freq='MS'): 200,
  Timestamp('2022-09-01 00:00:00', freq='MS'): 175.0,
  Timestamp('2022-10-01 00:00:00', freq='MS'): 175.0,
  Timestamp('2022-11-01 00:00:00', freq='MS'): 175.0,
  Timestamp('2022-12-01 00:00:00', freq='MS'): 175.0},
 'Vol': {Timestamp('2018-01-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-02-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-03-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-04-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-05-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-06-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-07-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-08-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-09-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-10-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-11-01 00:00:00', freq='MS'): nan,
  Timestamp('2018-12-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-01-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-02-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-03-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-04-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-05-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-06-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-07-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-08-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-09-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-10-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-11-01 00:00:00', freq='MS'): nan,
  Timestamp('2019-12-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-01-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-02-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-03-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-04-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-05-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-06-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-07-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-08-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-09-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-10-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-11-01 00:00:00', freq='MS'): nan,
  Timestamp('2020-12-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-01-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-02-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-03-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-04-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-05-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-06-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-07-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-08-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-09-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-10-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-11-01 00:00:00', freq='MS'): nan,
  Timestamp('2021-12-01 00:00:00', freq='MS'): nan,
  Timestamp('2022-01-01 00:00:00', freq='MS'): 16000,
  Timestamp('2022-02-01 00:00:00', freq='MS'): 10000,
  Timestamp('2022-03-01 00:00:00', freq='MS'): 12000,
  Timestamp('2022-04-01 00:00:00', freq='MS'): 40000,
  Timestamp('2022-05-01 00:00:00', freq='MS'): 20222,
  Timestamp('2022-06-01 00:00:00', freq='MS'): 67885,
  Timestamp('2022-07-01 00:00:00', freq='MS'): 12345,
  Timestamp('2022-08-01 00:00:00', freq='MS'): 5654,
  Timestamp('2022-09-01 00:00:00', freq='MS'): 75334,
  Timestamp('2022-10-01 00:00:00', freq='MS'): 45653,
  Timestamp('2022-11-01 00:00:00', freq='MS'): 432467,
  Timestamp('2022-12-01 00:00:00', freq='MS'): 457543}}


df = pd.DataFrame.from_dict(data)
df['Indicator'] = False
df

Help would be much appreciated thanks!

CodePudding user response:

you should be able to directly extract the month from the timestamp index

df["Indicator"] =  df.index.month <= 6

and you can replace the 6 with the desired number of months

  • Related