0

I have DataFrame in following format:

                   Date     Open     High      Low    Close
0   2015-06-19 20:00:00  1201.60  1202.84  1201.55  1202.13
1   2015-06-19 21:00:00  1202.13  1202.50  1200.84  1200.88
2   2015-06-19 22:00:00  1200.88  1201.55  1200.61  1201.06
3   2015-06-19 23:00:00  1201.06  1201.26  1200.02  1200.57
4   2015-06-22 01:00:00  1200.57  1201.48  1197.04  1198.94
5   2015-06-22 02:00:00  1198.94  1199.79  1198.49  1199.34
6   2015-06-22 03:00:00  1199.34  1200.05  1198.64  1199.74
7   2015-06-22 04:00:00  1199.74  1200.34  1199.14  1199.66

I am trying to split this DataFrame by dates and after that i am trying to split dates in eveery 4 hours. Here is how i select DataFrame by date:

i = 0
this_date = df["Date"][i:i+1].values[0].split(" ")[0]
today = df[df["Date"].apply(lambda x: x.split(" ")[0]) == this_date]

Now i need to split today dataframe in every 4 hours. The last size will be 3 in total as it ends at 23:00

How can i do this? Are there any easy way or do i need to map over DataFrame and do it manually?

Don Coder
  • 526
  • 5
  • 24
  • 1
    i think you'll have to loop here over all 4h intervals and subselect the rows that fall into this interval, i.e. `subdf = df[numpy.and(df.time>interval_low, df.time – mzoll Apr 09 '18 at 12:25
  • 1
    Look at this SO post https://stackoverflow.com/a/44729807/6361531 – Scott Boston Apr 09 '18 at 12:44
  • @ScottBoston this doesn't look like the way i am looking for. Final chunk len of the day is 3 not 4. – Don Coder Apr 10 '18 at 08:48
  • I don't quite understand your logic. Every 4 hours a day would end up with six groups of four. 0123 4567 891011 12131415 16171819 20212223 Why does hour 23 only get three records? – Scott Boston Apr 10 '18 at 15:29

0 Answers0