-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split pandas functions into separate module.
This frees //labm8:sqlutil from the dependency on //third_party/py/pandas, which is currently broken. github.com/ChrisCummins/phd/issues/51 [Exported from 2391f3a74286a844b98a5537a0e17c4a19ddf324]
- Loading branch information
1 parent
a00529b
commit 4fa47bf
Showing
5 changed files
with
154 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2014-2019 Chris Cummins <[email protected]>. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Utility code for working with pandas.""" | ||
import pandas as pd | ||
import typing | ||
from absl import flags as absl_flags | ||
|
||
from labm8 import sqlutil | ||
|
||
FLAGS = absl_flags.FLAGS | ||
|
||
|
||
def QueryToDataFrame(session: sqlutil.Session, | ||
query: sqlutil.Query) -> pd.DataFrame: | ||
"""Read query results to a Pandas DataFrame. | ||
Args: | ||
session: A database session. | ||
query: The query to run. | ||
Returns: | ||
A Pandas DataFrame. | ||
""" | ||
return pd.read_sql(query.statement, session.bind) | ||
|
||
|
||
def ModelToDataFrame( | ||
session: sqlutil.Session, | ||
model, | ||
columns: typing.Optional[typing.List[str]] = None, | ||
query_identity=lambda q: q, | ||
): | ||
"""Construct and execute a query reads an object's fields to a dataframe. | ||
Args: | ||
session: A database session. | ||
model: A database mapped object. | ||
columns: A list of column names, where each element is a column mapped to | ||
the model. If not provided, all column names are used. | ||
query_identity: A function which takes the produced query and returns a | ||
query. Use this to implement filtering of the query results. | ||
Returns: | ||
A Pandas DataFrame with one column for each field. | ||
""" | ||
columns = columns or ColumnNames(model) | ||
query = session.query(*[getattr(model, column) for column in columns]) | ||
df = QueryToDataFrame(session, query_identity(query)) | ||
df.columns = columns | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2014-2019 Chris Cummins <[email protected]>. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Unit tests for //labm8:sqlutil.""" | ||
import sqlalchemy as sql | ||
from sqlalchemy.ext import declarative | ||
|
||
from labm8 import pdutil | ||
from labm8 import sqlutil | ||
|
||
|
||
def test_QueryToDataFrame_column_names(): | ||
"""Test that expected column names are set.""" | ||
base = declarative.declarative_base() | ||
|
||
class Table(base): | ||
__tablename__ = 'test' | ||
col_a = sql.Column(sql.Integer, primary_key=True) | ||
col_b = sql.Column(sql.Integer) | ||
|
||
db = sqlutil.Database('sqlite://', base) | ||
with db.Session() as s: | ||
df = pdutil.QueryToDataFrame(s, s.query(Table.col_a, Table.col_b)) | ||
|
||
assert list(df.columns.values) == ['col_a', 'col_b'] | ||
|
||
|
||
def test_ModelToDataFrame_column_names(): | ||
"""Test that expected column names are set.""" | ||
base = declarative.declarative_base() | ||
|
||
class Table(base): | ||
__tablename__ = 'test' | ||
col_a = sql.Column(sql.Integer, primary_key=True) | ||
col_b = sql.Column(sql.Integer) | ||
|
||
db = sqlutil.Database('sqlite://', base) | ||
with db.Session() as s: | ||
df = pdutil.ModelToDataFrame(s, Table) | ||
|
||
assert list(df.columns.values) == ['col_a', 'col_b'] | ||
|
||
|
||
def test_QueryToDataFrame_explicit_column_names(): | ||
"""Test that expected column names are set.""" | ||
base = declarative.declarative_base() | ||
|
||
class Table(base): | ||
__tablename__ = 'test' | ||
col_a = sql.Column(sql.Integer, primary_key=True) | ||
col_b = sql.Column(sql.Integer) | ||
|
||
db = sqlutil.Database('sqlite://', base) | ||
with db.Session() as s: | ||
df = pdutil.ModelToDataFrame(s, Table, ['col_b']) | ||
|
||
assert list(df.columns.values) == ['col_b'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters