I have Precipitation Data (1800 rows and 15k columns) for each cell and date. I want to find dates where a specific amount of rain (>15mm) was reached and count the days after this event were less rain (<1,1mm) occurred. Together with the amount of rain, start and end period, cell and other information stored in a new DataFrame.
I write a for loop that does this job but it took several days to finish ;(. I am a beginner of python so maybe there are some tips for other methods.
from datetime import datetime, timedelta, date
import datetime
import pandas as pd
#Existing Data
index_dates = pd.date_range(pd.datetime.today(), periods=10).tolist()
df = pd.DataFrame({'486335':[0,0,16,0,0,0,2,1,8,2],'486336':[2,1,8,0,11,16,0,1,6,8],'486337':[22,1,22,0,0,0,5,3,6,1]},index=index_dates)
columns = df.columns
counter_columns = 0
iteration = -1 #Iterations Steps
counter = 10 #10 precipitation values per column
duration = 0 #days with no or less than pp_max_1 rain
count = False
index_list = df.index #Index for updating df / Integear
period_range = 0 #Amount of days after Event without much rain Integear
period_amount = 0 #Amount of PP in dry days except event Integear
event_amount = 0.0 #Amount of heavy rainfall on the event date Float
pp = 0 #actual precipitation
pp_sum = 0.0 #mm
pp_min = 15.0 #mm min pp for start to count dry days until duration_min_after
pp_max_1 = 0.11 #max pp for 1 day while counting dry days
dry_days = 0 #dry days after event
for x in df:
for y in df[x]:
iteration = iteration + 1
if iteration == counter:
iteration = 0
counter_columns = counter_columns + 1
print("column :",counter_columns, "finished")
if y >= pp_min and count == False:
duration = duration + 1
count = True
start_period = index_list[iteration]
event_amount = y
index = iteration
pp_sum = pp_sum + y
elif y >= pp_min and count == True or y >= pp_max_1 and count == True:
end_period = index_list[iteration]
dry_periods = dry_periods.append({"start_period":start_period ,"end_period":end_period,"period_range":duration,"period_amount":pp_sum ,"event_amount":event_amount, "cell":columns[counter_columns]},ignore_index=True).sort_values('period_range',ascending=False)
duration = 0
count = False
pp_sum = 0
elif pp <= pp_max_1 and count == True:
duration = duration + 1
pp_sum = pp_sum + y
else:
continue
print(dry_periods)
The output looks like this
start_period end_period period_range \
0 2019-07-05 13:15:05.545 2019-07-09 13:15:05.545 4
1 2019-07-05 13:15:05.545 2019-07-09 13:15:05.545 4
2 2019-07-05 13:15:36.569 2019-07-09 13:15:36.569 4
3 2019-07-05 13:15:36.569 2019-07-09 13:15:36.569 4
4 2019-07-05 13:16:16.372 2019-07-09 13:16:16.372 4
5 2019-07-05 13:16:16.372 2019-07-09 13:16:16.372 4
6 2019-07-05 13:16:38.461 2019-07-09 13:16:38.461 4
15 2019-07-05 13:16:38.461 2019-07-09 13:16:38.461 4
7 2019-07-08 13:15:05.545 2019-07-10 13:15:05.545 2
8 2019-07-08 13:15:36.569 2019-07-10 13:15:36.569 2
9 2019-07-08 13:16:16.372 2019-07-10 13:16:16.372 2
10 2019-07-08 13:16:38.461 2019-07-10 13:16:38.461 2
11 2019-07-03 13:15:05.545 2019-07-04 13:15:05.545 1
12 2019-07-03 13:15:36.569 2019-07-04 13:15:36.569 1
13 2019-07-03 13:16:16.372 2019-07-04 13:16:16.372 1
14 2019-07-03 13:16:38.461 2019-07-04 13:16:38.461 1
period_amount event_amount cell
0 16.0 16 486335
1 22.0 22 486337
2 16.0 16 486335
3 22.0 22 486337
4 16.0 16 486335
5 22.0 22 486337
6 16.0 16 486335
15 22.0 22 486337
7 16.0 16 486336
8 16.0 16 486336
9 16.0 16 486336
10 16.0 16 486336
11 22.0 22 486337
12 22.0 22 486337
13 22.0 22 486337
14 22.0 22 486337
Aucun commentaire:
Enregistrer un commentaire