Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Rename allow_overflow to wrap_numerical #16807

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions py-polars/polars/_utils/construction/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions py-polars/polars/_utils/construction/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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
)
Expand Down
14 changes: 7 additions & 7 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,20 +1708,20 @@ def cast(
dtype: PolarsDataType | type[Any],
*,
strict: bool = True,
allow_overflow: bool = False,
wrap_numerical: bool = False,
) -> Self:
"""
r"""
Cast between data types.

Parameters
----------
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
--------
Expand All @@ -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:
"""
Expand Down
14 changes: 7 additions & 7 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3878,20 +3878,20 @@ def cast(
dtype: PolarsDataType | type[int] | type[float] | type[str] | type[bool],
*,
strict: bool = True,
allow_overflow: bool = False,
wrap_numerical: bool = False,
) -> Self:
"""
r"""
Cast between data types.

Parameters
----------
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
--------
Expand All @@ -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:
"""
Expand Down
4 changes: 2 additions & 2 deletions py-polars/src/expr/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ impl PyExpr {
fn null_count(&self) -> Self {
self.inner.clone().null_count().into()
}
fn cast(&self, data_type: Wrap<DataType>, strict: bool, allow_overflow: bool) -> Self {
fn cast(&self, data_type: Wrap<DataType>, 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
Expand Down
4 changes: 2 additions & 2 deletions py-polars/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ impl PySeries {
Ok(out)
}

fn cast(&self, dtype: Wrap<DataType>, strict: bool, allow_overflow: bool) -> PyResult<Self> {
let options = if allow_overflow {
fn cast(&self, dtype: Wrap<DataType>, strict: bool, wrap_numerical: bool) -> PyResult<Self> {
let options = if wrap_numerical {
CastOptions::Overflowing
} else if strict {
CastOptions::Strict
Expand Down