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

feat(python): Use a contravariant typevar for df.cast #19499

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions py-polars/polars/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __arrow_c_stream__(self, requested_schema: object | None = None) -> object:
# selector type, and related collection/sequence
SelectorType: TypeAlias = "_selector_proxy_"
ColumnNameOrSelector: TypeAlias = Union[str, SelectorType]
ColumnNameOrSelectorOrDataType = TypeVar("ColumnNameOrSelectorOrDataType", bound=ColumnNameOrSelector | PolarsDataType, contravariant=True)

# User-facing string literal types
# The following all have an equivalent Rust enum with the same name
Expand Down
3 changes: 2 additions & 1 deletion py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
ClosedInterval,
ColumnFormatDict,
ColumnNameOrSelector,
ColumnNameOrSelectorOrDataType,
ColumnTotalsDefinition,
ColumnWidthsDefinition,
ComparisonOperator,
Expand Down Expand Up @@ -7620,7 +7621,7 @@ def drop_in_place(self, name: str) -> Series:
def cast(
self,
dtypes: (
Mapping[ColumnNameOrSelector | PolarsDataType, PolarsDataType]
Mapping[ColumnNameOrSelectorOrDataType, PolarsDataType]
| PolarsDataType
),
*,
Expand Down
3 changes: 2 additions & 1 deletion py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
AsofJoinStrategy,
ClosedInterval,
ColumnNameOrSelector,
ColumnNameOrSelectorOrDataType,
CsvQuoteStyle,
EngineType,
ExplainFormat,
Expand Down Expand Up @@ -2932,7 +2933,7 @@ def cache(self) -> LazyFrame:
def cast(
self,
dtypes: (
Mapping[ColumnNameOrSelector | PolarsDataType, PolarsDataType]
Mapping[ColumnNameOrSelectorOrDataType, PolarsDataType]
| PolarsDataType
),
*,
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/operations/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,13 @@ def test_cast_consistency() -> None:
).to_dict(as_series=False) == {"a": [0.0], "b": ["0.0"], "c": ["0.0"]}


def test_cast_schema() -> None:
schema = pl.Schema({"v": pl.UInt8()})
df = pl.DataFrame({"v": [1, 2, 3]}).cast(schema)
assert df.collect_schema() == schema
assert df["v"].eq(pl.Series("v", [1, 2, 3], dtype=pl.UInt8())).all()


def test_cast_int_to_string_unsets_sorted_flag_19424() -> None:
s = pl.Series([1, 2]).set_sorted()
assert s.flags["SORTED_ASC"]
Expand Down
Loading