From d818ce3109b43ea9325cd557e31b495e1ec91d72 Mon Sep 17 00:00:00 2001 From: Yasha Bubnov Date: Thu, 7 Nov 2024 15:27:35 +0100 Subject: [PATCH] Update library to support python 3.12 and pandas>=2.0 This patch fixes a unescaped string symbols, like "\s", which are raising SyntaxError in Python 3.12 by replacing strings with raw strings. Also patch changes series iteration `iteritems()`, which was removed in Pandas>=2.0 and replaces it with `iloc` iterator, which is available in both, Pandas<2.0 and Pandas>=2.0 Patch also bumps version of the library to 0.3.7. --- pandas_schema/schema.py | 2 +- pandas_schema/validation.py | 4 ++-- pandas_schema/version.py | 2 +- test/test_validation.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas_schema/schema.py b/pandas_schema/schema.py index 0648d6c..e0471ee 100644 --- a/pandas_schema/schema.py +++ b/pandas_schema/schema.py @@ -64,7 +64,7 @@ def validate(self, df: pd.DataFrame, columns: typing.List[str] = None) -> typing # We associate the column objects in the schema with data frame series either by name or by position, depending # on the value of self.ordered if self.ordered: - series = [x[1] for x in df.iteritems()] + series = [df.iloc[:, i] for i in range(len(df.columns))] column_pairs = zip(series, self.columns) else: column_pairs = [] diff --git a/pandas_schema/validation.py b/pandas_schema/validation.py index 5f7c763..95c7d6f 100644 --- a/pandas_schema/validation.py +++ b/pandas_schema/validation.py @@ -329,7 +329,7 @@ def default_message(self): return 'contains trailing whitespace' def validate(self, series: pd.Series) -> pd.Series: - return ~series.astype(str).str.contains('\s+$') + return ~series.astype(str).str.contains(r'\s+$') class LeadingWhitespaceValidation(_SeriesValidation): @@ -345,7 +345,7 @@ def default_message(self): return 'contains leading whitespace' def validate(self, series: pd.Series) -> pd.Series: - return ~series.astype(str).str.contains('^\s+') + return ~series.astype(str).str.contains(r'^\s+') class IsDistinctValidation(_SeriesValidation): diff --git a/pandas_schema/version.py b/pandas_schema/version.py index 4596d03..d93912e 100644 --- a/pandas_schema/version.py +++ b/pandas_schema/version.py @@ -1 +1 @@ -__version__ = '0.3.6' +__version__ = '0.3.7' diff --git a/test/test_validation.py b/test/test_validation.py index fc40100..4bb4d8c 100644 --- a/test/test_validation.py +++ b/test/test_validation.py @@ -345,7 +345,7 @@ def test_invalid_dates(self): class StringRegexMatch(ValidationTestBase): def setUp(self): - self.validator = MatchesPatternValidation('^.+\.txt$') + self.validator = MatchesPatternValidation(r'^.+\.txt$') def test_valid_strings(self): self.validate_and_compare( @@ -408,7 +408,7 @@ class CompiledRegexMatch(ValidationTestBase): """ def setUp(self): - self.validator = MatchesPatternValidation(re.compile('^.+\.txt$', re.IGNORECASE)) + self.validator = MatchesPatternValidation(re.compile(r'^.+\.txt$', re.IGNORECASE)) def test_valid_strings(self): self.validate_and_compare(