Skip to content

Commit

Permalink
feat: use to_pyarrow instead of to_pandas in the interactive repr
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and kszucs committed Oct 16, 2023
1 parent f758228 commit 72aa573
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 74 deletions.
1 change: 1 addition & 0 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def configure_connection(dbapi_connection, connection_record):
with contextlib.suppress(duckdb.InvalidInputException):
duckdb.execute("SELECT ?", (1,))

engine.dialect._backslash_escapes = False
super().do_connect(engine)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion ibis/expr/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __repr__(self) -> str:
self._repr(),
]
return "\n".join(lines)
return capture.get()
return capture.get().rstrip()

def __reduce__(self):
return (self.__class__, (self._arg,))
Expand Down
12 changes: 6 additions & 6 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def cast(self, target_type: Any) -> Value:
│ 18.7 │
│ 17.4 │
│ 18.0 │
nan
NULL
│ 19.3 │
│ 20.6 │
│ 17.8 │
Expand Down Expand Up @@ -335,7 +335,7 @@ def typeof(self) -> ir.StringValue:
│ 39.1 │
│ 39.5 │
│ 40.3 │
nan
NULL
│ 36.7 │
└────────────────┘
>>> vals.typeof()
Expand Down Expand Up @@ -610,7 +610,7 @@ def notin(self, values: Value | Sequence[Value]) -> ir.BooleanValue:
│ 18.7 │
│ 17.4 │
│ 18.0 │
nan
NULL
│ 19.3 │
└───────────────┘
>>> t.bill_depth_mm.notin([18.7, 18.1])
Expand Down Expand Up @@ -770,7 +770,7 @@ def isnull(self) -> ir.BooleanValue:
│ 18.7 │
│ 17.4 │
│ 18.0 │
nan
NULL
│ 19.3 │
└───────────────┘
>>> t.bill_depth_mm.isnull()
Expand Down Expand Up @@ -805,7 +805,7 @@ def notnull(self) -> ir.BooleanValue:
│ 18.7 │
│ 17.4 │
│ 18.0 │
nan
NULL
│ 19.3 │
└───────────────┘
>>> t.bill_depth_mm.notnull()
Expand Down Expand Up @@ -1100,7 +1100,7 @@ def group_concat(
│ 39.1 │ 18.7 │
│ 39.5 │ 17.4 │
│ 40.3 │ 18.0 │
nan nan
NULL NULL
│ 36.7 │ 19.3 │
└────────────────┴───────────────┘
>>> t.bill_length_mm.group_concat()
Expand Down
22 changes: 12 additions & 10 deletions ibis/expr/types/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def mutate(
Examples
--------
>>> import ibis
>>> import ibis.selectors as s
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch()
>>> t
Expand All @@ -185,7 +186,7 @@ def mutate(
│ Adelie │ Torgersen │ 39.1 │ 18.7 │ 181 │ … │
│ Adelie │ Torgersen │ 39.5 │ 17.4 │ 186 │ … │
│ Adelie │ Torgersen │ 40.3 │ 18.0 │ 195 │ … │
│ Adelie │ Torgersen │ nan nan │ NULL │ … │
│ Adelie │ Torgersen │ NULL NULL │ NULL │ … │
│ Adelie │ Torgersen │ 36.7 │ 19.3 │ 193 │ … │
│ Adelie │ Torgersen │ 39.3 │ 20.6 │ 190 │ … │
│ Adelie │ Torgersen │ 38.9 │ 17.8 │ 181 │ … │
Expand All @@ -201,22 +202,23 @@ def mutate(
... centered_bill_len=ibis._.bill_length_mm
... - ibis._.bill_length_mm.mean()
... )
... .order_by(s.all())
... )
┏━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
┃ species ┃ bill_length_mm ┃ centered_bill_len ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
│ string │ float64 │ float64 │
├─────────┼────────────────┼───────────────────┤
│ Adelie │ 40.9 │ 2.108609 │
│ Adelie │ 42.7 │ 3.908609 │
│ Adelie │ 39.8 │ 1.008609 │
│ Adelie │ 36.5 │ -2.291391 │
│ Adelie │ 36.7 │ -2.091391 │
│ Adelie │ 39.3 │ 0.508609 │
│ Adelie │ 38.9 │ 0.108609 │
│ Adelie │ 39.2 │ 0.408609 │
│ Adelie │ 32.1 │ -6.691391 │
│ Adelie │ 33.1 │ -5.691391 │
│ Adelie │ 33.5 │ -5.291391 │
│ Adelie │ 34.0 │ -4.791391 │
│ Adelie │ 34.1 │ -4.691391 │
│ Adelie │ 42.0 │ 3.208609 │
│ Adelie │ 34.4 │ -4.391391 │
│ Adelie │ 34.5 │ -4.291391 │
│ Adelie │ 34.6 │ -4.191391 │
│ Adelie │ 34.6 │ -4.191391 │
│ Adelie │ 35.0 │ -3.791391 │
│ … │ … │ … │
└─────────┴────────────────┴───────────────────┘
Expand Down
32 changes: 27 additions & 5 deletions ibis/expr/types/pretty.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

import datetime
import json
from functools import singledispatch
from math import isfinite
from urllib.parse import urlparse

import rich
from rich.align import Align
from rich.console import Console
from rich.style import Style
from rich.text import Text

import ibis
Expand All @@ -18,8 +18,7 @@
simple_console = Console(force_terminal=False)


@singledispatch
def format_values(dtype, values):
def _format_nested(values):
interactive = ibis.options.repr.interactive
return [
rich.pretty.Pretty(
Expand All @@ -32,6 +31,29 @@ def format_values(dtype, values):
]


@singledispatch
def format_values(dtype, values):
return _format_nested(values)


@format_values.register(dt.Map)
def _(dtype, values):
return _format_nested([None if v is None else dict(v) for v in values])


@format_values.register(dt.JSON)
def _(dtype, values):
def try_json(v):
if v is None:
return None
try:
return json.loads(v)
except Exception:
return v

return _format_nested([try_json(v) for v in values])


@format_values.register(dt.Boolean)
@format_values.register(dt.UUID)
def _(dtype, values):
Expand Down Expand Up @@ -233,7 +255,7 @@ def to_rich_table(table, console_width=None):

# Compute the data and return a pandas dataframe
nrows = ibis.options.repr.interactive.max_rows
result = table.limit(nrows + 1).execute()
result = table.limit(nrows + 1).to_pyarrow()

# Now format the columns in order, stopping if the console width would
# be exceeded.
Expand All @@ -243,7 +265,7 @@ def to_rich_table(table, console_width=None):
remaining = console_width - 1 # 1 char for left boundary
for name, dtype in table.schema().items():
formatted, min_width, max_width = format_column(
dtype, result[name].iloc[:nrows].to_list()
dtype, result[name].to_pylist()[:nrows]
)
dtype_str = format_dtype(dtype)
if ibis.options.repr.interactive.show_types and not isinstance(
Expand Down
Loading

0 comments on commit 72aa573

Please sign in to comment.