Skip to content

Commit

Permalink
Merge pull request #57 from xmnlab/master
Browse files Browse the repository at this point in the history
 Append common actions to the PandasClient
  • Loading branch information
xmnlab committed Jun 5, 2018
2 parents e30aca7 + 99b17ec commit c3dfc8b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
64 changes: 64 additions & 0 deletions ibis/pandas/client.py
@@ -1,5 +1,6 @@
from __future__ import absolute_import

import re
import six
import toolz
import numpy as np
Expand All @@ -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
Expand Down Expand Up @@ -321,6 +323,68 @@ def compile(self, expr, *args, **kwargs):
def database(self, name=None):
return PandasDatabase(name, self)

def list_tables(self, like=None):
tables = list(self.dictionary.keys())
if like is not None:
pattern = re.compile(like)
return list(filter(lambda t: pattern.findall(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:
dtypes = ibis_schema_to_pandas(schema)
df = schema.apply_to(
pd.DataFrame(columns=list(map(toolz.first, dtypes)))
)

self.dictionary[table_name] = df

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 sch.infer(self.dictionary[table_name])

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 bool(self.list_tables(like=name))

@property
def version(self):
return parse_version(pd.__version__)
Expand Down
19 changes: 19 additions & 0 deletions ibis/pandas/tests/test_client.py
Expand Up @@ -36,12 +36,31 @@ def test_client_table_repr(table):
assert 'PandasTable' in repr(table)


def test_load_data(client):
client.load_data('testing', tm.makeDataFrame())
assert client.exists_table('testing')
assert client.get_schema('testing')


def test_create_table(client):
client.create_table('testing', obj=tm.makeDataFrame())
assert client.exists_table('testing')
client.create_table('testingschema', schema=client.get_schema('testing'))
assert client.exists_table('testingschema')


def test_literal(client):
lit = ibis.literal(1)
result = client.execute(lit)
assert result == 1


def test_list_tables(client):
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')
Expand Down

0 comments on commit c3dfc8b

Please sign in to comment.