Skip to content

Commit

Permalink
Append common actions to the PandasClient
Browse files Browse the repository at this point in the history
This pull request adds list_tables, load_data, and create_table
attributes for the PandasClient.

Author: tonyfast <tony.fast@gmail.com>

Closes ibis-project#1464 from tonyfast/pandas-ddl and squashes the following commits:

cd045b5 [tonyfast] Fix flake spacings
fd92308 [tonyfast] Merge remote-tracking branch 'ibis/master' into pandas-ddl
9e0f582 [tonyfast] Include a get_schema attribute and tests for create_table
9490ddd [tonyfast] Merge remote-tracking branch 'ibis/master' into pandas-ddl
dc3bb12 [tonyfast] A simpler exists_table method
5647eea [tonyfast] Resolve some flake8 issues
88997b1 [tonyfast] Add a test for list_tables(like="")
4dd37af [tonyfast] Add a get_schema method to the pandas client and fix for review
9db2db3 [tonyfast] Add spaces after function definition and simplify list_tables test.
def4e16 [tonyfast] Include tests for loading data with the pandas backend.
9d7aad7 [tonyfast] Add list_tables, load_data, and create_table to the pandas client
  • Loading branch information
tonyfast authored and cpcloud committed Jun 4, 2018
1 parent 9722a54 commit f7392fa
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
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
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
Original file line number Diff line number Diff line change
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 f7392fa

Please sign in to comment.