From ef16b3b26c0b74849f7d116f6ab0d787fd2c64bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bel=C3=A1k?= Date: Fri, 13 Oct 2023 12:18:54 +0200 Subject: [PATCH] fix: suppress warnings in data type inference (#179) Pandas 2.0 `pd.to_datetime` warns upon unsuccessful inference of date format. The warning is unwanted since we use the success/failure of conversion to datetime to determine whether a column is a datetime, i.e. knowingly call the function on data which might not contain datetimes. --- edvart/data_types.py | 5 ++++- edvart/report_sections/dataset_overview.py | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/edvart/data_types.py b/edvart/data_types.py index 17b50e0..cba19b9 100644 --- a/edvart/data_types.py +++ b/edvart/data_types.py @@ -1,3 +1,4 @@ +import warnings from enum import IntEnum import numpy as np @@ -179,7 +180,9 @@ def is_date(series: pd.Series) -> bool: if contains_numerics: return False try: - converted_series = pd.to_datetime(series.dropna(), errors="coerce") + with warnings.catch_warnings(): + warnings.simplefilter("ignore", category=UserWarning) + converted_series = pd.to_datetime(series.dropna(), errors="coerce") except ValueError: return False return converted_series.notna().all() diff --git a/edvart/report_sections/dataset_overview.py b/edvart/report_sections/dataset_overview.py index d46a26a..bfee502 100644 --- a/edvart/report_sections/dataset_overview.py +++ b/edvart/report_sections/dataset_overview.py @@ -378,6 +378,7 @@ def required_imports(self) -> List[str]: "from enum import IntEnum", "import numpy as np", "from IPython.display import display", + "import warnings", ] def add_cells(self, cells: List[Dict[str, Any]], df: pd.DataFrame) -> None: