'Python - aggregate data at 30 second intervals to 12 hour intervals
I have some data in a python data series like this, there are rows for every 30 secs 24 hours a day
2022-03-31 23:57:30+10:00 163.440459
2022-03-31 23:58:00+10:00 163.521460
2022-03-31 23:58:30+10:00 163.608255
2022-03-31 23:59:00+10:00 163.698986
2022-03-31 23:59:30+10:00 163.790968
I need to aggregate the data to 12 hour intervals specifically 6:00am and 6:00pm for each day ie
2022-03-31 06:00:00+10:00 sum of values for group by
2022-03-31 18:00:00+10:00 sum of values for group by
No idea how to start and ideas welcomed thank you
Solution 1:[1]
I made something to cover your description of how you need it to work.
num = open('r.txt', 'r').read().splitlines() #this is the list that was provided on a txt
list6 = [] #vars for later
list18 = []
days = []
for line in num: #looks throug the lines
if line != '': #makes sure its not a blank
day = line[5:7] #gets the day from line
days.append(day) # saves the day
transform = line[11:13] #gets the hour from line
value = line.split(" ")[1] #gets the aggregrated value
compare = int(transform) #transforms to work with easyer
if compare >= 18 or compare < 6: #checks if its after or equal to hour 18 or less than 6
list18.append(float(value))
else: #else its writes it down
list6.append(float(value))
print(f'2022-{days[0]}-31 06:00:00+10:00 {list6}') #result part1
print(f'2022-{days[0]}-31 18:00:00+10:00 {list18}') #result part2
It works with messing with the string arrangement.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
