From 0b3e70af5a2741dae9581c0c2f2e7c8c61aa2b0d Mon Sep 17 00:00:00 2001 From: armutlutost Date: Tue, 15 Sep 2026 18:18:58 +0300 Subject: [PATCH 1/3] Add CTable query examples --- src/blosc2/ctable.py | 58 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/blosc2/ctable.py b/src/blosc2/ctable.py index 2ffaa67bd..0db0797ef 100644 --- a/src/blosc2/ctable.py +++ b/src/blosc2/ctable.py @@ -13923,6 +13923,37 @@ def sort_by( TypeError If a column used as a sort key does not support ordering (e.g. complex numbers). + + Examples + -------- + Create a small table and materialise a plain sort:: + + >>> from dataclasses import dataclass + >>> @dataclass + ... class Record: + ... name: str = blosc2.field(blosc2.string(max_length=8)) + ... score: int = blosc2.field(blosc2.int64()) + ... group: int = blosc2.field(blosc2.int64()) + >>> t = blosc2.CTable( + ... Record, + ... new_data=[("a", 30, 1), ("b", 10, 2), ("c", 20, 1), ("d", 40, 2)], + ... ) + >>> t.sort_by("score")["score"][:].tolist() + [10, 20, 30, 40] + + Sorting a filtered table stays lazy until rows are read:: + + >>> filtered = t.where(t.group == 1) + >>> ranked = filtered.sort_by("score") + >>> print(ranked.base is not None) + True + >>> print(ranked["score"][:].tolist()) + [20, 30] + + Multiple keys are applied from left to right:: + + >>> t.sort_by(["group", "score"], ascending=[True, False])["name"][:].tolist() + ['a', 'c', 'd', 'b'] """ if inplace and view: raise ValueError("inplace=True and view=True are mutually exclusive.") @@ -15712,14 +15743,33 @@ def where( # noqa: C901 Examples -------- + Start with a small table:: + + >>> from dataclasses import dataclass + >>> @dataclass + ... class Record: + ... name: str = blosc2.field(blosc2.string(max_length=8)) + ... score: int = blosc2.field(blosc2.int64()) + ... group: int = blosc2.field(blosc2.int64()) + >>> t = blosc2.CTable( + ... Record, + ... new_data=[("a", 30, 1), ("b", 10, 2), ("c", 20, 1), ("d", 40, 2)], + ... ) + Filter using a string expression:: - view = t.where("value * category >= 150") - slim = t.where("value * category >= 150", columns=["value", "category"]) + >>> view = t.where("score > 15") + >>> print(view.base is t, view.nrows) + True 3 + >>> slim = t.where("score > 15", columns=["name", "score"]) + >>> print(slim.col_names) + ['name', 'score'] - Filter using column arithmetic:: + Compound predicates can use column expressions:: - view = t.where((t.value * t.category) >= 150) + >>> selected = t.where((t.group == 1) & (t.score >= 25)) + >>> print(selected["name"][:].tolist()) + ['a'] Blosc2 lazy functions can be used in column expressions:: From 92bbf167a8f8be5ebcca84b48b00ce9711ad3e20 Mon Sep 17 00:00:00 2001 From: Francesc Alted Date: Thu, 24 Sep 2026 13:36:33 +0200 Subject: [PATCH 2/3] Correct import statement for dataclass in ctable.py Fix import statement formatting in ctable.py Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/blosc2/ctable.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/blosc2/ctable.py b/src/blosc2/ctable.py index 0db0797ef..60139258c 100644 --- a/src/blosc2/ctable.py +++ b/src/blosc2/ctable.py @@ -13928,7 +13928,8 @@ def sort_by( -------- Create a small table and materialise a plain sort:: - >>> from dataclasses import dataclass +>>> import blosc2 +>>> from dataclasses import dataclass >>> @dataclass ... class Record: ... name: str = blosc2.field(blosc2.string(max_length=8)) From 2abb9c656e1175c4ec3ca8ae4860ac2492ba35df Mon Sep 17 00:00:00 2001 From: Francesc Alted Date: Thu, 24 Sep 2026 13:38:21 +0200 Subject: [PATCH 3/3] Fix formatting in ctable.py examples --- src/blosc2/ctable.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/blosc2/ctable.py b/src/blosc2/ctable.py index 60139258c..fc899d659 100644 --- a/src/blosc2/ctable.py +++ b/src/blosc2/ctable.py @@ -13928,8 +13928,8 @@ def sort_by( -------- Create a small table and materialise a plain sort:: ->>> import blosc2 ->>> from dataclasses import dataclass + >>> import blosc2 + >>> from dataclasses import dataclass >>> @dataclass ... class Record: ... name: str = blosc2.field(blosc2.string(max_length=8))