Skip to content

Commit

Permalink
Split pandas functions into separate module.
Browse files Browse the repository at this point in the history
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
ChrisCummins committed Aug 23, 2019
1 parent 3e4852c commit 125dbb3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 44 deletions.
23 changes: 22 additions & 1 deletion labm8/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,28 @@ py_test(
],
)

py_library(
name = "pdutil",
srcs = ["pdutil.py"],
visibility = ["//visibility:public"],
deps = [
":app",
":sqlutil",
"//third_party/py/pandas",
],
)

py_test(
name = "pdutil_test",
srcs = ["pdutil_test.py"],
deps = [
":pdutil",
":sqlutil",
":test",
"//third_party/py/sqlalchemy",
],
)

py_library(
name = "ppar",
srcs = ["ppar.py"],
Expand Down Expand Up @@ -627,7 +649,6 @@ py_library(
":pbutil",
":text",
"//third_party/py/absl",
"//third_party/py/pandas",
"//third_party/py/sqlalchemy",
],
)
Expand Down
46 changes: 3 additions & 43 deletions labm8/sqlutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utility code for working with sqlalchemy."""
import time

import collections
import contextlib
import pathlib
import time
import typing

import pandas as pd
import sqlalchemy as sql
import typing
from absl import flags as absl_flags
from sqlalchemy import func
from sqlalchemy import orm
Expand Down Expand Up @@ -468,45 +467,6 @@ def __repr__(self) -> str:
return self.url


def QueryToDataFrame(session: Session, query: 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: 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


class TablenameFromClassNameMixin(object):
"""A class mixin which derives __tablename__ from the class name.
Expand Down

0 comments on commit 125dbb3

Please sign in to comment.