-
Notifications
You must be signed in to change notification settings - Fork 20
feat: Refine dy.Categorical to support pl.Categories
#378
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
Merged
Oliver Borchert (borchero)
merged 10 commits into
remove-io-serialization
from
refine-categoricals
Jul 31, 2026
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
56ef794
feat: Refine dy.Categorical to support pl.Categories
delsner d60cee0
refine tests
delsner dcd6c72
nit
delsner 883b2e9
Fix
delsner 6267c85
Updates
borchero 5405d03
Merge branch 'remove-io-serialization' into refine-categoricals
borchero 85d10d1
Merge branch 'remove-io-serialization' into refine-categoricals
borchero 2f1c6cd
Fix tests
borchero 1497e18
Merge branch 'remove-io-serialization' into refine-categoricals
borchero 13f6297
Merge branch 'remove-io-serialization' into refine-categoricals
borchero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright (c) QuantCo 2025-2026 | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| from typing import Literal | ||
|
|
||
| import polars as pl | ||
| import pytest | ||
|
|
||
| import dataframely as dy | ||
| from dataframely.testing.factory import create_schema | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "column, expected_dtype", | ||
| [ | ||
| (dy.Categorical(), pl.Categorical()), | ||
| ( | ||
| dy.Categorical(dy.Categories("c", namespace="ns")), | ||
| pl.Categorical(pl.Categories("c", namespace="ns")), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_categories_dtype(column: dy.Categorical, expected_dtype: pl.DataType) -> None: | ||
| assert column.dtype == expected_dtype | ||
|
|
||
|
|
||
| def test_categories_equality() -> None: | ||
| assert dy.Categories("c") == dy.Categories("c") | ||
| assert dy.Categories("c") != dy.Categories("d") | ||
| assert dy.Categories("c", physical="u8") != dy.Categories("c", physical="u16") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("physical", "expected"), | ||
| [("u8", pl.UInt8), ("u16", pl.UInt16), ("u32", pl.UInt32)], | ||
| ) | ||
| def test_categories_to_polars_physical( | ||
| physical: Literal["u8", "u16", "u32"], expected: pl.DataType | ||
| ) -> None: | ||
| assert dy.Categories("c", physical=physical).to_polars().physical() == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "column", | ||
| [dy.Categorical(), dy.Categorical(dy.Categories("c", namespace="ns"))], | ||
| ) | ||
| @pytest.mark.parametrize("df_type", [pl.DataFrame, pl.LazyFrame]) | ||
| def test_valid( | ||
| df_type: type[pl.DataFrame] | type[pl.LazyFrame], | ||
| column: dy.Categorical, | ||
| ) -> None: | ||
| schema = create_schema("test", {"a": column}) | ||
| df = df_type({"a": ["x", "y", "x"]}).cast(column.dtype) | ||
| assert schema.is_valid(df) | ||
|
|
||
|
|
||
| def test_matches() -> None: | ||
| column = dy.Categorical(dy.Categories("c", physical="u16")) | ||
| expr = pl.col("a") | ||
| assert column.matches(dy.Categorical(dy.Categories("c", physical="u16")), expr) | ||
| assert not column.matches(dy.Categorical(dy.Categories("d", physical="u16")), expr) | ||
| assert not column.matches(dy.Categorical(dy.Categories("c", physical="u8")), expr) | ||
| assert not column.matches(dy.Categorical(), expr) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "column", | ||
| [dy.Categorical(), dy.Categorical(dy.Categories("c", namespace="ns"))], | ||
| ) | ||
| def test_as_dict_from_dict(column: dy.Categorical) -> None: | ||
| restored = dy.Categorical.from_dict(column.as_dict(pl.element())) | ||
| assert restored.categories == column.categories | ||
|
|
||
|
|
||
| def test_schema_serialization_roundtrip() -> None: | ||
| schema = create_schema( | ||
| "test", {"a": dy.Categorical(dy.Categories("c", namespace="ns"))} | ||
| ) | ||
| decoded = dy.deserialize_schema(schema.serialize()) | ||
| assert schema.matches(decoded) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.