diff --git a/ibis/pandas/client.py b/ibis/pandas/client.py index b62c5f249c3a..bf62e9926883 100644 --- a/ibis/pandas/client.py +++ b/ibis/pandas/client.py @@ -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,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__) diff --git a/ibis/pandas/tests/test_client.py b/ibis/pandas/tests/test_client.py index 9f3f3ec7e2f9..d538943c4a0d 100644 --- a/ibis/pandas/tests/test_client.py +++ b/ibis/pandas/tests/test_client.py @@ -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')