my.body.sleep: massive speedup for average temperature calculation

This commit is contained in:
Dima Gerasimov 2023-11-11 00:28:13 +00:00 committed by karlicoss
parent 37643c098f
commit bde43d6a7a

View file

@ -17,15 +17,21 @@ class Combine:
bdf = BM.dataframe()
temp = bdf['temp']
# sort index and drop nans, otherwise indexing with [start: end] gonna complain
temp = pd.Series(
temp.values,
index=pd.to_datetime(temp.index, utc=True)
).sort_index()
temp = temp.loc[temp.index.dropna()]
def calc_avg_temperature(row):
start = row['sleep_start']
end = row['sleep_end']
if pd.isna(start) or pd.isna(end):
return None
between = (start <= temp.index) & (temp.index <= end)
# on no temp data, returns nan, ok
return temp[between].mean()
return temp[start: end].mean()
df['avg_temp'] = df.apply(calc_avg_temperature, axis=1)
return df