lee.edward01
lee.edward01

Reputation: 473

python/pandas Pull month just once from dataframe index

Currently I have a dataframe that looks like this below:

df = 

            Portfolio Value  Reference Values
Date                                         
2017-12-08     1.116178e+06      1.131236e+06
2017-12-15     1.117073e+06      1.139133e+06
2017-12-22     1.132547e+06      1.148579e+06
2017-12-29     1.138442e+06      1.147916e+06
2018-01-05     1.164225e+06      1.176083e+06
2018-01-12     1.187353e+06      1.196030e+06
2018-01-19     1.189111e+06      1.201825e+06
2018-01-26     1.206821e+06      1.225157e+06
2018-02-02     1.158117e+06      1.176250e+06
2018-02-09     1.100856e+06      1.115643e+06
2018-02-16     1.147286e+06      1.161896e+06
2018-02-23     1.150850e+06      1.169671e+06
2018-03-02     1.131918e+06      1.142618e+06
2018-03-09     1.176213e+06      1.183319e+06
2018-03-16     1.169246e+06      1.168313e+06
2018-03-23     1.115895e+06      1.107276e+06
2018-03-30     1.142702e+06      1.129814e+06
2018-04-06     1.131622e+06      1.115807e+06
2018-04-13     1.155948e+06      1.140644e+06
2018-04-20     1.167440e+06      1.151066e+06
2018-04-27     1.161850e+06      1.147982e+06
2018-05-04     1.156383e+06      1.146540e+06
2018-05-11     1.188791e+06      1.175786e+06

I would like to get a list of months from the dataframe with weekly data in it but not repeating the same month. For example, right now in the beginning of the dataframe month 12 shows up four times, however in my list I would like for it to only show up once. This statement is valid for all months.

I would like the end result to look something like this:

monthList = [12, 1, 2, 3, 4, 5] 

my actual dataframes would have many years with varying start months and end months so I was hoping the end result would look something like this:

monthList = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5] #etc...

the monthList should start on the month in the dataframe and end on the month the dataframe ends on.

Upvotes: 1

Views: 36

Answers (1)

BENY
BENY

Reputation: 323226

Using unique

df.index.month.unique().tolist()
Out[99]: [12, 1, 2, 3, 4, 5]

For multiple years

pd.Series(df.index.month).groupby(df.index.year).unique()
Out[105]: 
Date
2017               [12]
2018    [1, 2, 3, 4, 5]
Name: Date, dtype: object

Upvotes: 4

Related Questions