New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Append common actions to the PandasClient #1464
Changes from 6 commits
9d7aad7
def4e16
9db2db3
4dd37af
88997b1
5647eea
dc3bb12
9490ddd
9e0f582
fd92308
cd045b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from __future__ import absolute_import | ||
|
||
import re | ||
import six | ||
import toolz | ||
import numpy as np | ||
|
@@ -9,6 +10,7 @@ | |
from multipledispatch import Dispatcher | ||
|
||
import ibis.client as client | ||
import ibis.common as com | ||
import ibis.expr.types as ir | ||
import ibis.expr.schema as sch | ||
import ibis.expr.datatypes as dt | ||
|
@@ -321,6 +323,64 @@ def compile(self, expr, *args, **kwargs): | |
def database(self, name=None): | ||
return PandasDatabase(name, self) | ||
|
||
def get_schema(self, table_name, database=None): | ||
""" | ||
Return a Schema object for the indicated table and database | ||
|
||
Parameters | ||
---------- | ||
table_name : string | ||
May be fully qualified | ||
database : string, default None | ||
|
||
Returns | ||
------- | ||
schema : ibis Schema | ||
""" | ||
return infer_pandas_schema(self.dictionary[table_name]) | ||
|
||
def list_tables(self, like=None): | ||
tables = list(self.dictionary.keys()) | ||
if like is not None: | ||
return list(filter(lambda t: re.search(like, t), tables)) | ||
return tables | ||
|
||
def load_data(self, table_name, obj, **kwargs): | ||
""" | ||
Parameters | ||
---------- | ||
table_name : string | ||
obj: pandas.DataFrame | ||
""" | ||
# kwargs is a catch all for any options required by other backends. | ||
self.dictionary[table_name] = pd.DataFrame(obj) | ||
|
||
def create_table(self, table_name, obj=None, schema=None): | ||
if obj is None and schema is None: | ||
raise com.IbisError('Must pass expr or schema') | ||
|
||
if obj is not None: | ||
df = pd.DataFrame(obj) | ||
else: | ||
df = schema.apply_to(pd.DataFrame()) | ||
|
||
self.dictionary[table_name] = df | ||
|
||
def exists_table(self, name): | ||
""" | ||
Determine if the indicated table or view exists | ||
|
||
Parameters | ||
---------- | ||
name : string | ||
database : string, default None | ||
|
||
Returns | ||
------- | ||
if_exists : boolean | ||
""" | ||
return name in self.list_tables() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think this should be |
||
|
||
@property | ||
def version(self): | ||
return parse_version(pd.__version__) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,12 +36,24 @@ def test_client_table_repr(table): | |
assert 'PandasTable' in repr(table) | ||
|
||
|
||
def test_load_data(client): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use two newlines after function definitions. |
||
client.load_data('testing', tm.makeDataFrame()) | ||
assert client.exists_table('testing') | ||
assert client.get_schema('testing') | ||
|
||
|
||
def test_literal(client): | ||
lit = ibis.literal(1) | ||
result = client.execute(lit) | ||
assert result == 1 | ||
|
||
|
||
def test_list_tables(client): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. two newlines |
||
assert client.list_tables(like='df_unknown') | ||
assert not client.list_tables(like='not_in_the_database') | ||
assert client.list_tables() | ||
|
||
|
||
def test_read_with_undiscoverable_type(client): | ||
with pytest.raises(TypeError): | ||
client.table('df_unknown') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for this?