-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa2c0a9
commit 7dfdd82
Showing
5 changed files
with
93 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,3 +1,47 @@ | ||
# SPDX-FileCopyrightText: 2024-present Dylan Lukes <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import polars as pl | ||
|
||
from renkon.core.model.type import rk_int | ||
from renkon.core.model.type.base import Type as RenkonType, rk_float, rk_str, rk_bool | ||
|
||
|
||
def polars_type_to_renkon_type(rk_ty: pl.PolarsDataType) -> RenkonType: | ||
""" | ||
Convert a Polars data type to a Renkon data type. | ||
""" | ||
if rk_ty.is_integer(): | ||
return rk_int | ||
|
||
if rk_ty.is_float(): | ||
return rk_float | ||
|
||
if rk_ty.is_(pl.String): | ||
return rk_str | ||
|
||
if rk_ty.is_(pl.Boolean): | ||
return rk_bool | ||
|
||
raise ValueError(f"Unsupported Polars data type: {rk_ty}") | ||
|
||
|
||
def renkon_type_to_polars_type(rk_ty: RenkonType) -> pl.PolarsDataType: | ||
""" | ||
Convert a Renkon data type to a Polars data type. | ||
""" | ||
|
||
if rk_ty.is_equal(rk_int): | ||
return pl.Int64 | ||
|
||
if rk_ty.is_equal(rk_float) or rk_ty.is_equal(rk_int | rk_float): | ||
return pl.Float64 | ||
|
||
if rk_ty.is_equal(rk_str): | ||
return pl.Utf8 | ||
|
||
if rk_ty.is_equal(rk_bool): | ||
return pl.Boolean | ||
|
||
raise ValueError(f"Unsupported Renkon data type: {rk_ty}") |
This file contains 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,28 @@ | ||
# SPDX-FileCopyrightText: 2024-present Dylan Lukes <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import polars as pl | ||
import pytest | ||
|
||
import renkon.core.model.type as rkty | ||
from renkon.core.model.type import polars_type_to_renkon_type, renkon_type_to_polars_type | ||
|
||
|
||
def test_polars_type_to_renkon_type(): | ||
assert polars_type_to_renkon_type(pl.Int64) == rkty.rk_int | ||
assert polars_type_to_renkon_type(pl.Float64) == rkty.rk_float | ||
assert polars_type_to_renkon_type(pl.Utf8) == rkty.rk_str | ||
assert polars_type_to_renkon_type(pl.Boolean) == rkty.rk_bool | ||
|
||
with pytest.raises(ValueError): | ||
polars_type_to_renkon_type(pl.Date) | ||
polars_type_to_renkon_type(pl.Time) | ||
polars_type_to_renkon_type(pl.Datetime) | ||
|
||
|
||
def test_renkon_type_to_polars_type(): | ||
assert renkon_type_to_polars_type(rkty.rk_int) == pl.Int64 | ||
assert renkon_type_to_polars_type(rkty.rk_float) == pl.Float64 | ||
assert renkon_type_to_polars_type(rkty.rk_str) == pl.Utf8 | ||
assert renkon_type_to_polars_type(rkty.rk_bool) == pl.Boolean |
This file contains 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