From f3dcbaa3455f67d639db91b1a74b249c1a68a292 Mon Sep 17 00:00:00 2001 From: apbassett <43486400+apbassett@users.noreply.github.com> Date: Tue, 15 Oct 2024 15:19:11 -0400 Subject: [PATCH] Warning logic update --- howso/utilities/feature_attributes/pandas.py | 33 +++++++++---------- .../feature_attributes/relational.py | 2 +- .../tests/test_infer_feature_attributes.py | 2 +- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/howso/utilities/feature_attributes/pandas.py b/howso/utilities/feature_attributes/pandas.py index 91e76b85..60d3ed1f 100644 --- a/howso/utilities/feature_attributes/pandas.py +++ b/howso/utilities/feature_attributes/pandas.py @@ -565,26 +565,23 @@ def _infer_date_attributes(self, feature_name: str) -> dict: def _infer_time_attributes(self, feature_name: str, user_time_format: str = None) -> dict: # Import this here to avoid circular import from howso.client.exceptions import HowsoError - # First check if the user provided a format - if user_time_format: + first_non_null = self._get_first_non_null(feature_name) + # If the type is datetime.time + if isinstance(first_non_null, datetime.time): + if user_time_format: + warnings.warn(f"Feature '{feature_name} is an instance of 'datetime.time', " + "so the user-provided format will be ignored.") + # Format string representation of datetime.time types + time_format = '%H:%M:%S' + # If the type is a string + elif user_time_format is not None: time_format = user_time_format - # If not, try to infer it else: - first_non_null = self._get_first_non_null(feature_name) - # If the type is datetime.time - if isinstance(first_non_null, datetime.time): - if user_time_format: - warnings.warn(f"Feature '{feature_name} is an instance of 'datetime.time', " - "so the user-provided format will be ignored.") - # String representation of datetime.time types - time_format = '%H:%M:%S' - # If the type is a string - else: - try: - time_format = infer_time_format(first_non_null) - except ValueError as e: - raise HowsoError(f"Please specify the format of feature '{feature_name}' in " - "'datetime_feature_formats'") from e + try: + time_format = infer_time_format(first_non_null) + except ValueError as e: + raise HowsoError(f"Please specify the format of feature '{feature_name}' in " + "'datetime_feature_formats'") from e return { 'type': 'cyclic', 'data_type': 'formatted_time', diff --git a/howso/utilities/feature_attributes/relational.py b/howso/utilities/feature_attributes/relational.py index fa932811..b8d9fcc2 100644 --- a/howso/utilities/feature_attributes/relational.py +++ b/howso/utilities/feature_attributes/relational.py @@ -648,7 +648,7 @@ def _infer_date_attributes(self, feature_name: str) -> Dict: def _infer_time_attributes(self, feature_name: str, user_time_format: str = None) -> dict: # Import this here to avoid circular import from howso.client.exceptions import HowsoError - # Although rare, it is plausible that a date field could be a + # Although rare, it is plausible that a time field could be a # primary- or foreign-key. if ( self._is_primary_key(feature_name) or diff --git a/howso/utilities/feature_attributes/tests/test_infer_feature_attributes.py b/howso/utilities/feature_attributes/tests/test_infer_feature_attributes.py index 1aeae857..d5e375b9 100644 --- a/howso/utilities/feature_attributes/tests/test_infer_feature_attributes.py +++ b/howso/utilities/feature_attributes/tests/test_infer_feature_attributes.py @@ -436,7 +436,7 @@ def test_infer_feature_bounds(data, tight_bounds, expected_bounds): """Test the infer_feature_bounds() method.""" df = pd.DataFrame(pd.Series(data), columns=['a']) features = infer_feature_attributes(df, tight_bounds=tight_bounds) - assert features['a']['type'] in ('continuous') + assert features['a']['type'] == 'continuous' assert 'bounds' in features['a'] assert features['a']['bounds'] == expected_bounds