From d3f255156001c96ce25dab797932f5c2f547be61 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 4 Oct 2020 00:00:25 +0100 Subject: [PATCH] core.pandas: check index in check_dataframe --- my/bluemaestro/__init__.py | 7 +++++-- my/core/pandas.py | 10 +++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/my/bluemaestro/__init__.py b/my/bluemaestro/__init__.py index 06bfdee..d2514a1 100755 --- a/my/bluemaestro/__init__.py +++ b/my/bluemaestro/__init__.py @@ -142,7 +142,8 @@ def stats() -> Stats: return stat(measurements) -from ..core.pandas import DataFrameT +from ..core.pandas import DataFrameT, check_dataframe as cdf +@cdf def dataframe() -> DataFrameT: """ %matplotlib gtk @@ -152,6 +153,8 @@ def dataframe() -> DataFrameT: # todo not sure why x axis time ticks are weird... df[:6269] works, whereas df[:6269] breaks... # either way, plot is not the best representation for the temperature I guess.. maybe also use bokeh? import pandas as pd # type: ignore - return pd.DataFrame(p._asdict() for p in measurements()).set_index('dt') + df = pd.DataFrame(p._asdict() for p in measurements()) + # todo not sure how it would handle mixed timezones?? + return df.set_index('dt') # todo test against an older db? diff --git a/my/core/pandas.py b/my/core/pandas.py index de9077a..8cf8e58 100644 --- a/my/core/pandas.py +++ b/my/core/pandas.py @@ -3,6 +3,7 @@ Various pandas helpers and convenience functions ''' # todo not sure if belongs to 'core'. It's certainly 'more' core than actual modules, but still not essential # NOTE: this file is meant to be importable without Pandas installed +from datetime import datetime from typing import Optional, TYPE_CHECKING, Any from . import warnings @@ -26,9 +27,9 @@ def check_dateish(s) -> Optional[str]: s = s.dropna() if len(s) == 0: return None - all_timestamps = s.apply(lambda x: isinstance(x, pd.Timestamp)).all() + all_timestamps = s.apply(lambda x: isinstance(x, (pd.Timestamp, datetime))).all() if all_timestamps: - return 'All values are pd.Timestamp, but dtype is not datetime. Most likely, you have mixed timezones' + return 'All values are timestamp-like, but dtype is not datetime. Most likely, you have mixed timezones' return None @@ -41,11 +42,10 @@ def check_dataframe(f: FuncT) -> FuncT: def wrapper(*args, **kwargs) -> DataFrameT: df = f(*args, **kwargs) # todo make super defensive? - # TODO check index as well? - for col, data in df.iteritems(): + for col, data in df.reset_index().iteritems(): res = check_dateish(data) if res is not None: - warnings.low(f"{f.__name__}, column '{col}': {res}") + warnings.low(f"{f.__module__}:{f.__name__}, column '{col}': {res}") return df # https://github.com/python/mypy/issues/1927 return wrapper # type: ignore[return-value]