I have a for loop that I would like to loop through and add a value once its done with the calculation in the loop. However, I cannot store it because I want to also store a date as an index that is created in the loop. The data comes from a CSV file. I want my index for the dataframe to be the date it is looping through. For example, if on the current iteration, the month = 1, and day=2 (im only working in 2021), then I want the index for that iteration to be '1/2/2021' and also insert the slope (the value I am storing) in the column 'dT/dt' at that date.
dT_dt = pd.DataFrame(columns=['Date', 'dT/dt']) # empy dataframe for storing
dT_dt.set_index('Date', inplace=True)
for month in range(1,13):
for day in range(1,32):
# Converts given sunrise and sunset times to datetime format
print(day)
sunrise_time = sunrise_sunset.loc[sunrise_sunset['Date'] == str(month)+'/'+str(day)+'/2021', 'Sunrise']
sunset_time = sunrise_sunset.loc[sunrise_sunset['Date'] == str(month)+'/'+str(day)+'/2021', 'Sunset']
sunrise_time = datetime.strptime(str(str(month)+'/'+str(day)+'/2021 '+sunrise_time).removeprefix(str(day-1)+' ').removesuffix('\nName: Sunrise, dtype: object'), "%m/%d/%Y %I:%M:%S %p")
sunset_time = datetime.strptime(str(str(month)+'/'+str(day)+'/2021 '+sunset_time).removeprefix(str(day-1)+' ').removesuffix('\nName: Sunset, dtype: object'), "%m/%d/%Y %I:%M:%S %p")
# Converts ASOS data times to datetime format
times = pd.DataFrame(raw_data['valid'])
times['valid'] = pd.to_datetime(times['valid'], format = '%m/%d/%Y %H:%M')
# Function to find the nearest ASOS time to the sunrise and sunset times
def nearest(items, pivot):
return min(items, key=lambda x: abs(x - pivot))
raw_sunrise_index_time=times.loc[times.valid == nearest(times.valid.to_list(), sunrise_time)].index[0]
raw_sunset_index_time=times.loc[times.valid == nearest(times.valid.to_list(), sunset_time)].index[0]
# Isolates temperatures between sunrise (+2) and sunset (-1)
raw_data_day = raw_data.iloc[raw_sunrise_index_time+2:raw_sunset_index_time]
raw_t = raw_data_day['tmpf']
# Conversions of data to determine linear regressions
raw_t_values = list(map(float, raw_t.values))
raw_t_index = list(map(float, raw_t.index))
# Determining the slope (dT/dt) of the iterated day
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(raw_t_index, raw_t_values)
# Stores dT/dt
dT_dt['dT/dt'] = slope