From de467aaf741ba3914b51ddfe4a7b8517c6b0e763 Mon Sep 17 00:00:00 2001 From: ritchie Date: Fri, 7 Jun 2024 16:06:55 +0200 Subject: [PATCH 1/3] refactor: Rename allow_overflow to wrap_numerical --- py-polars/polars/expr/expr.py | 12 ++++++------ py-polars/polars/series/series.py | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/py-polars/polars/expr/expr.py b/py-polars/polars/expr/expr.py index d306736c205c..2bfeda5bca0e 100644 --- a/py-polars/polars/expr/expr.py +++ b/py-polars/polars/expr/expr.py @@ -1708,7 +1708,7 @@ def cast( dtype: PolarsDataType | type[Any], *, strict: bool = True, - allow_overflow: bool = False, + wrap_numerical: bool = False, ) -> Self: """ Cast between data types. @@ -1718,10 +1718,10 @@ def cast( dtype DataType to cast to. strict - Throw an error if a cast could not be done (for instance, due to an - overflow). - allow_overflow - Don't check for numeric overflow and instead do a `wrapping` numeric cast. + If True invalid casts generate exceptions instead of `null`s. + wrap_numerical + If True numeric casts wrap overflowing values instead of + marking the cast as invalid. Examples -------- @@ -1747,7 +1747,7 @@ def cast( └─────┴─────┘ """ dtype = py_type_to_dtype(dtype) - return self._from_pyexpr(self._pyexpr.cast(dtype, strict, allow_overflow)) + return self._from_pyexpr(self._pyexpr.cast(dtype, strict, wrap_numerical)) def sort(self, *, descending: bool = False, nulls_last: bool = False) -> Self: """ diff --git a/py-polars/polars/series/series.py b/py-polars/polars/series/series.py index 8d9fc4bacbef..9c39b85b62b1 100644 --- a/py-polars/polars/series/series.py +++ b/py-polars/polars/series/series.py @@ -3878,7 +3878,7 @@ def cast( dtype: PolarsDataType | type[int] | type[float] | type[str] | type[bool], *, strict: bool = True, - allow_overflow: bool = False, + wrap_numerical: bool = False, ) -> Self: """ Cast between data types. @@ -3888,10 +3888,10 @@ def cast( dtype DataType to cast to. strict - Throw an error if a cast could not be done (for instance, due to an - overflow). - allow_overflow - Don't check for numeric overflow and instead do a `wrapping` numeric cast. + If True invalid casts generate exceptions instead of `null`s. + wrap_numerical + If True numeric casts wrap overflowing values instead of + marking the cast as invalid. Examples -------- @@ -3916,7 +3916,7 @@ def cast( """ # Do not dispatch cast as it is expensive and used in other functions. dtype = py_type_to_dtype(dtype) - return self._from_pyseries(self._s.cast(dtype, strict, allow_overflow)) + return self._from_pyseries(self._s.cast(dtype, strict, wrap_numerical)) def to_physical(self) -> Series: """ From dae9feb0479d2a937cf2e857559ec6c35c2c6af9 Mon Sep 17 00:00:00 2001 From: ritchie Date: Fri, 7 Jun 2024 16:13:05 +0200 Subject: [PATCH 2/3] inline --- py-polars/polars/expr/expr.py | 4 ++-- py-polars/polars/series/series.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/py-polars/polars/expr/expr.py b/py-polars/polars/expr/expr.py index 2bfeda5bca0e..8d0936965c59 100644 --- a/py-polars/polars/expr/expr.py +++ b/py-polars/polars/expr/expr.py @@ -1710,7 +1710,7 @@ def cast( strict: bool = True, wrap_numerical: bool = False, ) -> Self: - """ + r""" Cast between data types. Parameters @@ -1718,7 +1718,7 @@ def cast( dtype DataType to cast to. strict - If True invalid casts generate exceptions instead of `null`s. + If True invalid casts generate exceptions instead of `null`\s. wrap_numerical If True numeric casts wrap overflowing values instead of marking the cast as invalid. diff --git a/py-polars/polars/series/series.py b/py-polars/polars/series/series.py index 9c39b85b62b1..bb4d560c2b6d 100644 --- a/py-polars/polars/series/series.py +++ b/py-polars/polars/series/series.py @@ -3880,7 +3880,7 @@ def cast( strict: bool = True, wrap_numerical: bool = False, ) -> Self: - """ + r""" Cast between data types. Parameters @@ -3888,7 +3888,7 @@ def cast( dtype DataType to cast to. strict - If True invalid casts generate exceptions instead of `null`s. + If True invalid casts generate exceptions instead of `null`\s. wrap_numerical If True numeric casts wrap overflowing values instead of marking the cast as invalid. From c8bcd9f65e27b270e19549140fe9594f515aad79 Mon Sep 17 00:00:00 2001 From: ritchie Date: Mon, 10 Jun 2024 08:28:59 +0200 Subject: [PATCH 3/3] rename more --- py-polars/polars/_utils/construction/dataframe.py | 8 ++++---- py-polars/polars/_utils/construction/series.py | 8 ++++---- py-polars/src/expr/general.rs | 4 ++-- py-polars/src/series/mod.rs | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/py-polars/polars/_utils/construction/dataframe.py b/py-polars/polars/_utils/construction/dataframe.py index f05cbb8b9412..bc2a94785640 100644 --- a/py-polars/polars/_utils/construction/dataframe.py +++ b/py-polars/polars/_utils/construction/dataframe.py @@ -629,7 +629,7 @@ def _sequence_of_series_to_pydf( s = s.alias(column_names[i]) new_dtype = schema_overrides.get(column_names[i]) if new_dtype and new_dtype != s.dtype: - s = s.cast(new_dtype, strict=strict, allow_overflow=False) + s = s.cast(new_dtype, strict=strict, wrap_numerical=False) data_series.append(s._s) data_series = _handle_columns_arg(data_series, columns=column_names) @@ -766,7 +766,7 @@ def _sequence_of_pandas_to_pydf( pyseries = plc.pandas_to_pyseries(name=name, values=s) dtype = schema_overrides.get(name) if dtype is not None and dtype != pyseries.dtype(): - pyseries = pyseries.cast(dtype, strict=strict, allow_overflow=False) + pyseries = pyseries.cast(dtype, strict=strict, wrap_numerical=False) data_series.append(pyseries) return PyDataFrame(data_series) @@ -1354,7 +1354,7 @@ def series_to_pydf( new_dtype = next(iter(schema_overrides.values())) if new_dtype != data.dtype: data_series[0] = data_series[0].cast( - new_dtype, strict=strict, allow_overflow=False + new_dtype, strict=strict, wrap_numerical=False ) data_series = _handle_columns_arg(data_series, columns=column_names) @@ -1381,7 +1381,7 @@ def dataframe_to_pydf( for name, new_dtype in schema_overrides.items(): if new_dtype != existing_schema[name]: data_series[name] = data_series[name].cast( - new_dtype, strict=strict, allow_overflow=False + new_dtype, strict=strict, wrap_numerical=False ) series_cols = _handle_columns_arg(list(data_series.values()), columns=column_names) diff --git a/py-polars/polars/_utils/construction/series.py b/py-polars/polars/_utils/construction/series.py index aee1da92abd3..925f7b7be237 100644 --- a/py-polars/polars/_utils/construction/series.py +++ b/py-polars/polars/_utils/construction/series.py @@ -150,7 +150,7 @@ def sequence_to_pyseries( Decimal, ): if pyseries.dtype() != dtype: - pyseries = pyseries.cast(dtype, strict=strict, allow_overflow=False) + pyseries = pyseries.cast(dtype, strict=strict, wrap_numerical=False) return pyseries elif dtype == Struct: @@ -289,7 +289,7 @@ def sequence_to_pyseries( name, values, dtype, strict=strict ) if dtype != pyseries.dtype(): - pyseries = pyseries.cast(dtype, strict=False, allow_overflow=False) + pyseries = pyseries.cast(dtype, strict=False, wrap_numerical=False) return pyseries elif python_dtype == pl.Series: @@ -308,7 +308,7 @@ def sequence_to_pyseries( np.bool_(True), np.generic ): dtype = numpy_char_code_to_dtype(np.dtype(python_dtype).char) - return srs.cast(dtype, strict=strict, allow_overflow=False) + return srs.cast(dtype, strict=strict, wrap_numerical=False) else: return srs @@ -481,7 +481,7 @@ def arrow_to_pyseries( pys.rechunk(in_place=True) return ( - pys.cast(dtype, strict=strict, allow_overflow=False) + pys.cast(dtype, strict=strict, wrap_numerical=False) if dtype is not None else pys ) diff --git a/py-polars/src/expr/general.rs b/py-polars/src/expr/general.rs index 059df272829b..8f4c58743b98 100644 --- a/py-polars/src/expr/general.rs +++ b/py-polars/src/expr/general.rs @@ -260,10 +260,10 @@ impl PyExpr { fn null_count(&self) -> Self { self.inner.clone().null_count().into() } - fn cast(&self, data_type: Wrap, strict: bool, allow_overflow: bool) -> Self { + fn cast(&self, data_type: Wrap, strict: bool, wrap_numerical: bool) -> Self { let dt = data_type.0; - let options = if allow_overflow { + let options = if wrap_numerical { CastOptions::Overflowing } else if strict { CastOptions::Strict diff --git a/py-polars/src/series/mod.rs b/py-polars/src/series/mod.rs index d25c40d5d2d7..2f7d998fb401 100644 --- a/py-polars/src/series/mod.rs +++ b/py-polars/src/series/mod.rs @@ -710,8 +710,8 @@ impl PySeries { Ok(out) } - fn cast(&self, dtype: Wrap, strict: bool, allow_overflow: bool) -> PyResult { - let options = if allow_overflow { + fn cast(&self, dtype: Wrap, strict: bool, wrap_numerical: bool) -> PyResult { + let options = if wrap_numerical { CastOptions::Overflowing } else if strict { CastOptions::Strict