my.body.sleep: massive speedup for average temperature calculation
This commit is contained in:
parent
37643c098f
commit
bde43d6a7a
1 changed files with 8 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue