Skip to content

Commit d11a292

Browse files
authored
feat(export): implement the Column.to_list() API (#10498)
## Description of changes Implement `Column.to_list()` convenience method to avoid having to do `Column.to_pyarrow().to_pylist()` manually. ## Issues closed * Resolves #10102
1 parent abe378b commit d11a292

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ ibis/examples/pixi.lock linguist-generated=true
44
requirements-dev.txt linguist-generated=true
55
docs/_freeze/**/html.json linguist-generated=true
66
docs/**/*.excalidraw linguist-generated=true
7+
# GitHub syntax highlighting
8+
pixi.lock linguist-language=YAML linguist-generated=true

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,7 @@ docs/**/*.html
153153
.jupyterlite.doit.db
154154
docs/jupyter_lite_config.json
155155
*.quarto_ipynb
156+
157+
# pixi environments
158+
.pixi
159+
*.egg-info

ibis/backends/tests/test_export.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,22 @@ def test_column_to_memory(limit, awards_players, output_format, expected_column_
586586
method = methodcaller(f"to_{output_format}", limit=limit)
587587
res = method(awards_players.awardID)
588588
assert isinstance(res, getattr(mod, expected_column_type))
589-
assert (limit is not None and len(res) == limit) or len(
590-
res
591-
) == awards_players.count().execute()
589+
assert (
590+
(len(res) == limit)
591+
if limit is not None
592+
else len(res) == awards_players.count().execute()
593+
)
594+
595+
596+
@pytest.mark.parametrize("limit", limit_no_limit)
597+
def test_column_to_list(limit, awards_players):
598+
res = awards_players.awardID.to_list(limit=limit)
599+
assert isinstance(res, list)
600+
assert (
601+
(len(res) == limit)
602+
if limit is not None
603+
else len(res) == awards_players.count().execute()
604+
)
592605

593606

594607
@pytest.mark.parametrize("limit", no_limit)

ibis/expr/types/generic.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ def desc(self, nulls_first: bool = False) -> ir.Value:
12001200
return ops.SortKey(self, ascending=False, nulls_first=nulls_first).to_expr()
12011201

12021202
def to_pandas(self, **kwargs) -> pd.Series:
1203-
"""Convert a column expression to a pandas Series or scalar object.
1203+
"""Convert an expression to a pandas or scalar object.
12041204
12051205
Parameters
12061206
----------
@@ -1211,8 +1211,8 @@ def to_pandas(self, **kwargs) -> pd.Series:
12111211
--------
12121212
>>> import ibis
12131213
>>> ibis.options.interactive = True
1214-
>>> t = ibis.examples.penguins.fetch().limit(5)
1215-
>>> t.to_pandas()
1214+
>>> t = ibis.examples.penguins.fetch()
1215+
>>> t.to_pandas(limit=5)
12161216
species island bill_length_mm ... body_mass_g sex year
12171217
0 Adelie Torgersen 39.1 ... 3750.0 male 2007
12181218
1 Adelie Torgersen 39.5 ... 3800.0 female 2007
@@ -2684,6 +2684,24 @@ def nth(self, n: int | ir.IntegerValue) -> Column:
26842684
"""
26852685
return ops.NthValue(self, n).to_expr()
26862686

2687+
def to_list(self, **kwargs) -> list:
2688+
"""Convert a column expression to a list.
2689+
2690+
Parameters
2691+
----------
2692+
kwargs
2693+
Same as keyword arguments to [`to_pyarrow`](#ibis.expr.types.core.Expr.to_pyarrow)
2694+
2695+
Examples
2696+
--------
2697+
>>> import ibis
2698+
>>> ibis.options.interactive = True
2699+
>>> t = ibis.examples.penguins.fetch()
2700+
>>> t.bill_length_mm.to_list(limit=5)
2701+
[39.1, 39.5, 40.3, None, 36.7]
2702+
"""
2703+
return self.to_pyarrow(**kwargs).to_pylist()
2704+
26872705

26882706
@public
26892707
class UnknownValue(Value):

0 commit comments

Comments
 (0)