0

How do I create dataframes showing daily data for each year to create histograms in plotly?

                                Open        High        Low         Close       Volume      
   Date                         
   2019-06-26                  81.478964    81.789175   80.102395   80.422302   5731400  
   2019-06-27                  80.839148    81.411104   80.528937   81.129974   4111600 
   2019-06-28                  81.255994    81.682537   80.577409   81.527428   16436700
   .....
   2020-12-31                  81.255994    81.682537   80.577409   81.527428   16436700

I want my output for 2019 data frame to have all of 2019 transactions and my other dataframe to have all of 2020 transactions

SeaBean
  • 22,547
  • 3
  • 13
  • 25
  • Have a look at this answer https://stackoverflow.com/a/61339028/3376059 The last suggested option there will allow you to filter by year, I hope this is what you were asking (so if not, more elaboration could do). Also, please provide a way to re-create the dataframe. – OmerM25 Jun 27 '21 at 13:40
  • Can you be a little more specific, which values of OHLC do you want to graph? Do you have an example of the output you expect? – r-beginners Jun 27 '21 at 13:41
  • @OmerM25 That is exactly what I wanted. Do I delete my question? – the_brass_bottle Jun 27 '21 at 13:47

2 Answers2

0

As you have datetime index, you can use DatetimeIndex.year to get the year of index to filter the year, like below:

df_2019 = df.loc[df.index.year == 2019]
df_2020 = df.loc[df.index.year == 2020]

You can also use:

df_2019 = df.loc['2019-01-01':'2019-12-31']
df_2020 = df.loc['2020-01-01':'2020-12-31']
SeaBean
  • 22,547
  • 3
  • 13
  • 25
0

Try with

d = {x : y for x , y in df.groupby(df.index.year)}
BENY
  • 317,841
  • 20
  • 164
  • 234