Skip to content

Commit

Permalink
Include a get_schema attribute and tests for create_table
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfast committed Jun 4, 2018
1 parent 9490ddd commit 9e0f582
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
44 changes: 25 additions & 19 deletions ibis/pandas/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,28 +323,14 @@ 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))
pattern = re.compile(like)
return list(filter(lambda t: pattern.findall(t), tables))
return tables


def load_data(self, table_name, obj, **kwargs):
"""
Parameters
Expand All @@ -362,10 +348,29 @@ def create_table(self, table_name, obj=None, schema=None):
if obj is not None:
df = pd.DataFrame(obj)
else:
df = schema.apply_to(pd.DataFrame())
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
Expand All @@ -379,7 +384,8 @@ def exists_table(self, name):
-------
if_exists : boolean
"""
return name in self.dictionary
return bool(self.list_tables(like=name))


@property
def version(self):
Expand Down
5 changes: 5 additions & 0 deletions ibis/pandas/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def test_load_data(client):
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)
Expand Down

0 comments on commit 9e0f582

Please sign in to comment.