diff --git a/my/bluemaestro/__init__.py b/my/bluemaestro/__init__.py index 5230431..172164a 100755 --- a/my/bluemaestro/__init__.py +++ b/my/bluemaestro/__init__.py @@ -36,6 +36,7 @@ class Measurement(NamedTuple): # NOTE: the timezone should be set with respect to the export date!!! import pytz # type: ignore tz = pytz.timezone('Europe/London') +# TODO when I change tz, check the diff @mcachew(cache_path=cache_dir() / 'bluemaestro.cache') diff --git a/my/body/sleep/common.py b/my/body/sleep/common.py index 81879fe..0b6fa1c 100644 --- a/my/body/sleep/common.py +++ b/my/body/sleep/common.py @@ -7,10 +7,26 @@ class Combine: self.modules = modules @cdf - def dataframe(self) -> DataFrameT: + def dataframe(self, with_temperature: bool=True) -> DataFrameT: import pandas as pd # type: ignore # todo include 'source'? df = pd.concat([m.dataframe() for m in self.modules]) + + if with_temperature: + from ... import bluemaestro as BM + bdf = BM.dataframe() + temp = bdf['temp'] + + def calc_avg_temperature(row): + start = row['sleep_start'] + end = row['sleep_end'] + if pd.isna(start) or pd.isna(end): + return None + + # on no temp data, returns nan, ok + return temp[start: end].mean() + + df['avg_temp'] = df.apply(calc_avg_temperature, axis=1) return df def stats(self) -> Stats: