Skip to content
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

Closed
wants to merge 11 commits into from
60 changes: 60 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,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):
Copy link
Member

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?

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this should be name in self.dictionary. No reason to make this unnecessarily slow.


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


def test_load_data(client):
Copy link
Member

Choose a reason for hiding this comment

The 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):
Copy link
Member

Choose a reason for hiding this comment

The 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')
Expand Down