/
api.py
86 lines (62 loc) · 1.84 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import absolute_import
import toolz
import ibis
from ibis.pandas.client import PandasClient
from ibis.pandas.execution import execute, execute_node
from ibis.pandas.udf import udf
__all__ = ('connect', 'dialect', 'execute', 'udf')
def connect(dictionary):
"""Construct a pandas client from a dictionary of DataFrames.
Parameters
----------
dictionary : dict
Returns
-------
PandasClient
"""
return PandasClient(dictionary)
def from_dataframe(df, name='df', client=None):
"""
convenience function to construct an ibis table
from a DataFrame
EXPERIMENTAL API
Parameters
----------
df : DataFrame
name : str, default 'df'
client : Client, default new PandasClient
client dictionary will be mutated with the
name of the DataFrame
Returns
-------
Table
"""
if client is None:
return connect({name: df}).table(name)
client.dictionary[name] = df
return client.table(name)
def _flatten_subclass_tree(cls):
"""Return the set of all child classes of `cls`.
Parameters
----------
cls : Type
Returns
-------
frozenset[Type]
"""
subclasses = frozenset(cls.__subclasses__())
children = frozenset(toolz.concat(map(_flatten_subclass_tree, subclasses)))
return frozenset({cls}) | subclasses | children
class PandasExprTranslator:
# get the dispatched functions from the execute_node dispatcher and compute
# and flatten the type tree of the first argument which is always the Node
# subclass
_registry = frozenset(
toolz.concat(
_flatten_subclass_tree(types[0]) for types in execute_node.funcs
)
)
_rewrites = {}
class PandasDialect(ibis.sql.compiler.Dialect):
translator = PandasExprTranslator
PandasClient.dialect = dialect = PandasDialect