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

test(python): More test restructure #6961

Merged
merged 8 commits into from
Feb 17, 2023
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
1 change: 1 addition & 0 deletions py-polars/tests/unit/datatypes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test module for testing behaviour of specific data types in various operations."""
Original file line number Diff line number Diff line change
Expand Up @@ -61,61 +61,6 @@ def test_read_csv_categorical() -> None:
assert df["col1"].dtype == pl.Categorical


def test_categorical_lexical_sort() -> None:
df = pl.DataFrame(
{"cats": ["z", "z", "k", "a", "b"], "vals": [3, 1, 2, 2, 3]}
).with_columns(
[
pl.col("cats").cast(pl.Categorical).cat.set_ordering("lexical"),
]
)

out = df.sort(["cats"])
assert out["cats"].dtype == pl.Categorical
expected = pl.DataFrame(
{"cats": ["a", "b", "k", "z", "z"], "vals": [2, 3, 2, 3, 1]}
)
assert_frame_equal(out.with_columns(pl.col("cats").cast(pl.Utf8)), expected)
out = df.sort(["cats", "vals"])
expected = pl.DataFrame(
{"cats": ["a", "b", "k", "z", "z"], "vals": [2, 3, 2, 1, 3]}
)
assert_frame_equal(out.with_columns(pl.col("cats").cast(pl.Utf8)), expected)
out = df.sort(["vals", "cats"])

expected = pl.DataFrame(
{"cats": ["z", "a", "k", "b", "z"], "vals": [1, 2, 2, 3, 3]}
)
assert_frame_equal(out.with_columns(pl.col("cats").cast(pl.Utf8)), expected)


def test_categorical_lexical_ordering_after_concat() -> None:
with pl.StringCache():
ldf1 = (
pl.DataFrame([pl.Series("key1", [8, 5]), pl.Series("key2", ["fox", "baz"])])
.lazy()
.with_columns(
pl.col("key2").cast(pl.Categorical).cat.set_ordering("lexical")
)
)
ldf2 = (
pl.DataFrame(
[pl.Series("key1", [6, 8, 6]), pl.Series("key2", ["fox", "foo", "bar"])]
)
.lazy()
.with_columns(
pl.col("key2").cast(pl.Categorical).cat.set_ordering("lexical")
)
)
df = (
pl.concat([ldf1, ldf2])
.with_columns(pl.col("key2").cat.set_ordering("lexical"))
.collect()
)

df.sort(["key1", "key2"])


def test_cat_to_dummies() -> None:
df = pl.DataFrame({"foo": [1, 2, 3, 4], "bar": ["a", "b", "a", "c"]})
df = df.with_columns(pl.col("bar").cast(pl.Categorical))
Expand Down Expand Up @@ -295,23 +240,6 @@ def test_categorical_in_struct_nulls() -> None:
assert s[2] == {"job": "waiter", "counts": 1}


def test_sort_categoricals_6014() -> None:
with pl.StringCache():
# create basic categorical
df1 = pl.DataFrame({"key": ["bbb", "aaa", "ccc"]}).with_columns(
pl.col("key").cast(pl.Categorical)
)
# create lexically-ordered categorical
df2 = pl.DataFrame({"key": ["bbb", "aaa", "ccc"]}).with_columns(
pl.col("key").cast(pl.Categorical).cat.set_ordering("lexical")
)

out = df1.sort("key")
assert out.to_dict(False) == {"key": ["bbb", "aaa", "ccc"]}
out = df2.sort("key")
assert out.to_dict(False) == {"key": ["aaa", "bbb", "ccc"]}


def test_cast_inner_categorical() -> None:
dtype = pl.List(pl.Categorical)
out = pl.Series("foo", [["a"], ["a", "b"]]).cast(dtype)
Expand Down
Loading