diff --git a/my/body/sleep/common.py b/my/body/sleep/common.py index 7bc1021..e84c8d5 100644 --- a/my/body/sleep/common.py +++ b/my/body/sleep/common.py @@ -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