Skip to content

Commit 11ed5a5

Browse files
Merge pull request #711 from armutlutost/docs/650-ctable-query-examples
Add runnable examples to CTable where and sort_by
2 parents c2f1408 + 2abb9c6 commit 11ed5a5

1 file changed

Lines changed: 55 additions & 4 deletions

File tree

‎src/blosc2/ctable.py‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14314,6 +14314,38 @@ def sort_by(
1431414314
TypeError
1431514315
If a column used as a sort key does not support ordering
1431614316
(e.g. complex numbers).
14317+
14318+
Examples
14319+
--------
14320+
Create a small table and materialise a plain sort::
14321+
14322+
>>> import blosc2
14323+
>>> from dataclasses import dataclass
14324+
>>> @dataclass
14325+
... class Record:
14326+
... name: str = blosc2.field(blosc2.string(max_length=8))
14327+
... score: int = blosc2.field(blosc2.int64())
14328+
... group: int = blosc2.field(blosc2.int64())
14329+
>>> t = blosc2.CTable(
14330+
... Record,
14331+
... new_data=[("a", 30, 1), ("b", 10, 2), ("c", 20, 1), ("d", 40, 2)],
14332+
... )
14333+
>>> t.sort_by("score")["score"][:].tolist()
14334+
[10, 20, 30, 40]
14335+
14336+
Sorting a filtered table stays lazy until rows are read::
14337+
14338+
>>> filtered = t.where(t.group == 1)
14339+
>>> ranked = filtered.sort_by("score")
14340+
>>> print(ranked.base is not None)
14341+
True
14342+
>>> print(ranked["score"][:].tolist())
14343+
[20, 30]
14344+
14345+
Multiple keys are applied from left to right::
14346+
14347+
>>> t.sort_by(["group", "score"], ascending=[True, False])["name"][:].tolist()
14348+
['a', 'c', 'd', 'b']
1431714349
"""
1431814350
if inplace and view:
1431914351
raise ValueError("inplace=True and view=True are mutually exclusive.")
@@ -16134,14 +16166,33 @@ def where( # noqa: C901
1613416166

1613516167
Examples
1613616168
--------
16169+
Start with a small table::
16170+
16171+
>>> from dataclasses import dataclass
16172+
>>> @dataclass
16173+
... class Record:
16174+
... name: str = blosc2.field(blosc2.string(max_length=8))
16175+
... score: int = blosc2.field(blosc2.int64())
16176+
... group: int = blosc2.field(blosc2.int64())
16177+
>>> t = blosc2.CTable(
16178+
... Record,
16179+
... new_data=[("a", 30, 1), ("b", 10, 2), ("c", 20, 1), ("d", 40, 2)],
16180+
... )
16181+
1613716182
Filter using a string expression::
1613816183

16139-
view = t.where("value * category >= 150")
16140-
slim = t.where("value * category >= 150", columns=["value", "category"])
16184+
>>> view = t.where("score > 15")
16185+
>>> print(view.base is t, view.nrows)
16186+
True 3
16187+
>>> slim = t.where("score > 15", columns=["name", "score"])
16188+
>>> print(slim.col_names)
16189+
['name', 'score']
1614116190

16142-
Filter using column arithmetic::
16191+
Compound predicates can use column expressions::
1614316192

16144-
view = t.where((t.value * t.category) >= 150)
16193+
>>> selected = t.where((t.group == 1) & (t.score >= 25))
16194+
>>> print(selected["name"][:].tolist())
16195+
['a']
1614516196

1614616197
Blosc2 lazy functions can be used in column expressions::
1614716198

0 commit comments

Comments
 (0)