1

i have a day dataframe below. i wanna select 3 hour consecutive values 4 times from 06:00 by increasing 15min.(using for loop) i tried using datetime.time(h,m,s) but don't know how to select after 3hours datetime setting. and still not good to using datetime & timedelta ..

date        time    
2018-01-26  00:00:00    -9.64
            00:15:00   -10.08
            00:30:00    -8.87
            00:45:00    -9.91
            01:00:00    -9.91
            01:15:00    -9.93
            01:30:00    -9.55
            01:45:00    -9.51
            02:00:00    -9.75
            02:15:00    -9.44
            02:30:00    -9.62
            02:45:00    -9.39
            03:00:00    -9.39
            03:15:00   -10.90
            03:30:00   -10.21
            03:45:00    -9.28
            04:00:00    -9.96
            04:15:00   -10.19
            04:30:00   -10.20
            04:45:00    -9.85
            05:00:00   -10.33
            05:15:00   -10.18
            05:30:00   -10.81
            05:45:00   -10.51
            06:00:00   -10.41
            06:15:00   -10.49
            06:30:00   -10.13
            06:45:00   -10.36
            07:00:00   -10.71
            07:15:00   -12.11
            07:30:00   -10.76
            07:45:00   -10.76
            08:00:00   -11.63
            08:15:00   -11.18
            08:30:00   -10.49
            08:45:00   -11.18
            09:00:00   -10.67
            09:15:00   -10.60
            09:30:00   -10.36
            09:45:00    -9.39
            10:00:00    -9.77
            10:15:00    -9.54
            10:30:00    -8.99
            10:45:00    -9.01
            11:00:00   -10.01
Name: out_temp, dtype: float64

the results should be like this

for loop 1

date        time    
2018-01-26  06:00:00   -10.41
            06:15:00   -10.49
            06:30:00   -10.13
            06:45:00   -10.36
            07:00:00   -10.71
            07:15:00   -12.11
            07:30:00   -10.76
            07:45:00   -10.76
            08:00:00   -11.63
            08:15:00   -11.18
            08:30:00   -10.49
            08:45:00   -11.18
            09:00:00   -10.67
Name: out_temp, dtype: float64

for loop2

date        time    
2018-01-26  06:15:00   -10.49
            06:30:00   -10.13
            06:45:00   -10.36
            07:00:00   -10.71
            07:15:00   -12.11
            07:30:00   -10.76
            07:45:00   -10.76
            08:00:00   -11.63
            08:15:00   -11.18
            08:30:00   -10.49
            08:45:00   -11.18
            09:00:00   -10.67
            09:15:00   -10.60
Name: out_temp, dtype: float64

... for loop4

how can i do it?

jerry han
  • 425
  • 4
  • 15

1 Answers1

1

You can loop 4 times, increment start and end time, select and append to dictionary of DataFrames or list of dataFrames:

from datetime import date, datetime, time, timedelta

start = time(6,0,0)
idx = pd.IndexSlice
d = {}
for x in range(4):
    #https://stackoverflow.com/a/656394/2901002
    end = (datetime.combine(date.today(), start) + timedelta(hours=3)).time()
    #print (start, end)
    d[x] = df.loc[idx[:, start: end]]
    start = (datetime.combine(date.today(), start) + timedelta(minutes=15)).time()

print (d[0])
date        time    
2018-01-26  06:00:00   -10.41
            06:15:00   -10.49
            06:30:00   -10.13
            06:45:00   -10.36
            07:00:00   -10.71
            07:15:00   -12.11
            07:30:00   -10.76
            07:45:00   -10.76
            08:00:00   -11.63
            08:15:00   -11.18
            08:30:00   -10.49
            08:45:00   -11.18
            09:00:00   -10.67
Name: out_temp, dtype: float64

print (d[1])
date        time    
2018-01-26  06:15:00   -10.49
            06:30:00   -10.13
            06:45:00   -10.36
            07:00:00   -10.71
            07:15:00   -12.11
            07:30:00   -10.76
            07:45:00   -10.76
            08:00:00   -11.63
            08:15:00   -11.18
            08:30:00   -10.49
            08:45:00   -11.18
            09:00:00   -10.67
            09:15:00   -10.60
Name: out_temp, dtype: float64

print (d[2])
date        time    
2018-01-26  06:30:00   -10.13
            06:45:00   -10.36
            07:00:00   -10.71
            07:15:00   -12.11
            07:30:00   -10.76
            07:45:00   -10.76
            08:00:00   -11.63
            08:15:00   -11.18
            08:30:00   -10.49
            08:45:00   -11.18
            09:00:00   -10.67
            09:15:00   -10.60
            09:30:00   -10.36
Name: out_temp, dtype: float64

print (d[3])
date        time    
2018-01-26  06:45:00   -10.36
            07:00:00   -10.71
            07:15:00   -12.11
            07:30:00   -10.76
            07:45:00   -10.76
            08:00:00   -11.63
            08:15:00   -11.18
            08:30:00   -10.49
            08:45:00   -11.18
            09:00:00   -10.67
            09:15:00   -10.60
            09:30:00   -10.36
            09:45:00    -9.39
Name: out_temp, dtype: float64

from datetime import date, datetime, time, timedelta

start = time(6,0,0)
idx = pd.IndexSlice
L = []
for x in range(4):
    #https://stackoverflow.com/a/656394/2901002
    end = (datetime.combine(date.today(), start) + timedelta(hours=3)).time()
    #print (start, end)
    L.append(df.loc[idx[:, start: end]])
    start = (datetime.combine(date.today(), start) + timedelta(minutes=15)).time()


print (L[0])
print (L[1])
print (L[2])
print (L[3])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252