Skip to content

Commit

Permalink
Change imports to "import pantab as pt" (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd authored Jan 28, 2024
1 parent 4409f75 commit 83c5bb4
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 103 deletions.
18 changes: 9 additions & 9 deletions benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pandas as pd

import pantab
import pantab as pt


class TimeSuite:
Expand Down Expand Up @@ -57,36 +57,36 @@ def setup(self):
)

path = "test.hyper"
pantab.frame_to_hyper(df, path, table="test")
pt.frame_to_hyper(df, path, table="test")

return df

def time_write_frame(self):
pantab.frame_to_hyper(self.df, "dummy.hyper", table="dummy")
pt.frame_to_hyper(self.df, "dummy.hyper", table="dummy")

def time_read_frame(self):
pantab.frame_from_hyper("test.hyper", table="test")
pt.frame_from_hyper("test.hyper", table="test")


class TimeWriteLong:
def setup(self):
self.df = pd.DataFrame(np.ones((10_000_000, 1)), columns=["a"])

def time_write_frame(self):
pantab.frame_to_hyper(self.df, "dummy.hyper", table="dummy")
pt.frame_to_hyper(self.df, "dummy.hyper", table="dummy")

def peakmem_write_frame(self):
pantab.frame_to_hyper(self.df, "dummy.hyper", table="dummy")
pt.frame_to_hyper(self.df, "dummy.hyper", table="dummy")


class TimeReadLong:
def setup(self):
df = pd.DataFrame(np.ones((10_000_000, 1)), columns=["a"])
path = "test.hyper"
pantab.frame_to_hyper(df, path, table="test")
pt.frame_to_hyper(df, path, table="test")

def time_read_frame(self):
pantab.frame_from_hyper("test.hyper", table="test")
pt.frame_from_hyper("test.hyper", table="test")

def peakmem_read_frame(self):
pantab.frame_from_hyper("test.hyper", table="test")
pt.frame_from_hyper("test.hyper", table="test")
4 changes: 2 additions & 2 deletions benchmarks/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import numpy as np
import pandas as pd

import pantab
import pantab as pt

if __name__ == "__main__":
df = pd.DataFrame(np.ones((100_000_000, 1)), columns=["a"])
start = time.time()
pantab.frame_to_hyper(df, "test.hyper", table="test")
pt.frame_to_hyper(df, "test.hyper", table="test")
end = time.time()
print(f"Execution took: {end - start}")
48 changes: 24 additions & 24 deletions doc/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ Writing to a Hyper Extract
.. code-block:: python
import pandas as pd
import pantab
import pantab as pt
df = pd.DataFrame([
["dog", 4],
["cat", 4],
], columns=["animal", "num_of_legs"])
pantab.frame_to_hyper(df, "example.hyper", table="animals")
pt.frame_to_hyper(df, "example.hyper", table="animals")
The above example will write out to a file named "example.hyper", which Tableau can then report off of.

Expand All @@ -25,9 +25,9 @@ Reading a Hyper Extract

.. code-block:: python
import pantab
import pantab as pt
df = pantab.frame_from_hyper("example.hyper", table="animals")
df = pt.frame_from_hyper("example.hyper", table="animals")
print(df)
Working with Schemas
Expand All @@ -38,7 +38,7 @@ By default tables will be written to the "public" schema. You can control this b
.. code-block:: python
import pandas as pd
import pantab
import pantab as pt
from tableauhyperapi import TableName
# Let's write somewhere besides the default public schema
Expand All @@ -49,10 +49,10 @@ By default tables will be written to the "public" schema. You can control this b
["cat", 4],
], columns=["animal", "num_of_legs"])
pantab.frame_to_hyper(df, "example.hyper", table=table)
pt.frame_to_hyper(df, "example.hyper", table=table)
# Can also be round-tripped
df2 = pantab.frame_from_hyper("example.hyper", table=table)
df2 = pt.frame_from_hyper("example.hyper", table=table)
.. note::

Expand All @@ -67,18 +67,18 @@ Reading and Writing Multiple Tables
.. code-block:: python
import pandas as pd
import pantab
import pantab as pt
from tableauhyperapi import TableName
dict_of_frames = {
"table1": pd.DataFrame([[1, 2]], columns=list("ab")),
TableName("non_public_schema", "table2"): pd.DataFrame([[3, 4]], columns=list("cd")),
}
pantab.frames_to_hyper(dict_of_frames, "example.hyper")
pt.frames_to_hyper(dict_of_frames, "example.hyper")
# Can also be round-tripped
result = pantab.frames_from_hyper("example.hyper")
result = pt.frames_from_hyper("example.hyper")
.. note::
Expand All @@ -93,19 +93,19 @@ By default, ``frame_to_hyper`` and ``frames_to_hyper`` will fully drop and reloa
.. code-block:: python
import pandas as pd
import pantab
import pantab as pt
df = pd.DataFrame([
["dog", 4],
["cat", 4],
], columns=["animal", "num_of_legs"])
pantab.frame_to_hyper(df, "example.hyper", table="animals")
pt.frame_to_hyper(df, "example.hyper", table="animals")
new_data = pd.DataFrame([["moose", 4]], columns=["animal", "num_of_legs"])
# Instead of overwriting the animals table, we can append via table_mode
pantab.frame_to_hyper(df, "example.hyper", table="animals", table_mode="a")
pt.frame_to_hyper(df, "example.hyper", table="animals", table_mode="a")
Please note that ``table_mode="a"`` will create the table(s) if they do not already exist.

Expand All @@ -120,7 +120,7 @@ With ``frame_from_hyper_query``, one can execute SQL queries against a Hyper fil
.. code-block:: python
import pandas as pd
import pantab
import pantab as pt
df = pd.DataFrame([
["dog", 4],
Expand All @@ -129,15 +129,15 @@ With ``frame_from_hyper_query``, one can execute SQL queries against a Hyper fil
["centipede", 100],
], columns=["animal", "num_of_legs"])
pantab.frame_to_hyper(df, "example.hyper", table="animals")
pt.frame_to_hyper(df, "example.hyper", table="animals")
# Read a subset of the data from the Hyper file
query = """
SELECT animal
FROM animals
WHERE num_of_legs > 4
"""
df = pantab.frame_from_hyper_query("example.hyper", query)
df = pt.frame_from_hyper_query("example.hyper", query)
print(df)
# Let Hyper do an aggregation for us - it could also do joins, window queries, ...
Expand All @@ -146,7 +146,7 @@ With ``frame_from_hyper_query``, one can execute SQL queries against a Hyper fil
FROM animals
GROUP BY num_of_legs
"""
df = pantab.frame_from_hyper_query("example.hyper", query)
df = pt.frame_from_hyper_query("example.hyper", query)
print(df)
Expand All @@ -170,7 +170,7 @@ By reusing the same ``HyperProcess`` for multiple operations, we also save a few
.. code-block:: python
import pandas as pd
import pantab
import pantab as pt
from tableauhyperapi import HyperProcess, Telemetry
df = pd.DataFrame([
Expand All @@ -181,11 +181,11 @@ By reusing the same ``HyperProcess`` for multiple operations, we also save a few
parameters = {"log_config": "", "default_database_version": "1"}
with HyperProcess(Telemetry.SEND_USAGE_DATA_TO_TABLEAU, parameters=parameters) as hyper:
# Insert some initial data
pantab.frame_to_hyper(df, "example.hyper", table="animals", hyper_process=hyper)
pt.frame_to_hyper(df, "example.hyper", table="animals", hyper_process=hyper)
# Append additional data to the same table using `table_mode="a"`
new_data = pd.DataFrame([["moose", 4]], columns=["animal", "num_of_legs"])
pantab.frame_to_hyper(df, "example.hyper", table="animals", table_mode="a", hyper_process=hyper)
pt.frame_to_hyper(df, "example.hyper", table="animals", table_mode="a", hyper_process=hyper)
Providing your own Hyper Connection
Expand All @@ -201,7 +201,7 @@ Hence, pantab also allows you to pass in a HyperAPI connection instead of the na
.. code-block:: python
import pandas as pd
import pantab
import pantab as pt
from tableauhyperapi import HyperProcess, Telemetry, Connection, CreateMode
df = pd.DataFrame([
Expand All @@ -212,16 +212,16 @@ Hence, pantab also allows you to pass in a HyperAPI connection instead of the na
path = "example.hyper"
with HyperProcess(Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper:
pantab.frames_to_hyper({"animals": df}, path, hyper_process=hyper)
pt.frames_to_hyper({"animals": df}, path, hyper_process=hyper)
with Connection(hyper.endpoint, path, CreateMode.NONE) as connection:
query = """
SELECT animal
FROM animals
WHERE num_of_legs > 4
"""
many_legs_df = pantab.frame_from_hyper_query(connection, query)
many_legs_df = pt.frame_from_hyper_query(connection, query)
print(many_legs_df)
all_animals = pantab.frame_from_hyper(connection, table="animals")
all_animals = pt.frame_from_hyper(connection, table="animals")
print(all_animals)
40 changes: 20 additions & 20 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ This was generated from the output of the below code on Manjaro Linux with 4.6 G
import numpy as np
import pandas as pd
import pantab
import pantab as pt
from tableauhyperapi import (
Connection,
CreateMode,
Expand All @@ -54,30 +54,30 @@ This was generated from the output of the below code on Manjaro Linux with 4.6 G
)
def write_via_pantab(proc: HyperProcess, n: int):
def write_via_pantab(n: int):
df = pd.DataFrame({"column": np.ones(n, dtype=np.int16)})
pantab.frame_to_hyper(df, "example.hyper", table="table", hyper_process=proc)
pantab.frame_to_hyper(df, "example.hyper", table="table")
def write_via_hyperapi(proc: HyperProcess, n: int):
def write_via_hyperapi(n: int):
data_to_insert = np.ones(n, dtype=np.int16)
with HyperProcess(telemetry=Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper:
table = TableDefinition(
table_name=TableName("table"),
columns=[TableDefinition.Column(name="column", type=SqlType.int())],
)
table = TableDefinition(
table_name=TableName("table"),
columns=[TableDefinition.Column(name="column", type=SqlType.int())],
)
with Connection(
endpoint=proc.endpoint,
database="example.hyper",
create_mode=CreateMode.CREATE_AND_REPLACE,
) as conn:
conn.catalog.create_table(table)
with Inserter(conn, table) as inserter:
for data in data_to_insert:
inserter.add_row([data])
inserter.execute()
with Connection(
endpoint=hyper.endpoint,
database="example.hyper",
create_mode=CreateMode.CREATE_AND_REPLACE,
) as conn:
conn.catalog.create_table(table)
with Inserter(conn, table) as inserter:
for data in data_to_insert:
inserter.add_row([data])
inserter.execute()
ns = (100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000)
Expand Down
5 changes: 2 additions & 3 deletions pantab/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import pyarrow as pa
import tableauhyperapi as tab_api

import pantab._types as pantab_types
import pantab.src.pantab as libpantab # type: ignore

TableType = Union[str, tab_api.Name, tab_api.TableName]


def frame_from_hyper_query(
source: Union[str, pathlib.Path],
Expand Down Expand Up @@ -40,7 +39,7 @@ def frame_from_hyper_query(
def frame_from_hyper(
source: Union[str, pathlib.Path],
*,
table: TableType,
table: pantab_types.TableNameType,
return_type: Literal["pandas", "polars", "pyarrow"] = "pandas",
):
"""See api.rst for documentation"""
Expand Down
2 changes: 1 addition & 1 deletion pantab/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import tableauhyperapi as tab_api

TableType = Union[str, tab_api.Name, tab_api.TableName]
TableNameType = Union[str, tab_api.Name, tab_api.TableName]
6 changes: 3 additions & 3 deletions pantab/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def frame_to_hyper(
df,
database: Union[str, pathlib.Path],
*,
table: pantab_types.TableType,
table: pantab_types.TableNameType,
table_mode: Literal["a", "w"] = "w",
json_columns: list[str] = None,
geo_columns: list[str] = None,
Expand All @@ -68,7 +68,7 @@ def frame_to_hyper(


def frames_to_hyper(
dict_of_frames: dict[pantab_types.TableType, Any],
dict_of_frames: dict[pantab_types.TableNameType, Any],
database: Union[str, pathlib.Path],
*,
table_mode: Literal["a", "w"] = "w",
Expand All @@ -87,7 +87,7 @@ def frames_to_hyper(
if table_mode == "a" and pathlib.Path(database).exists():
shutil.copy(database, tmp_db)

def convert_to_table_name(table: pantab_types.TableType):
def convert_to_table_name(table: pantab_types.TableNameType):
# nanobind expects a tuple of (schema, table) strings
if isinstance(table, (str, tab_api.Name)) or not table.schema_name:
table = tab_api.TableName("public", table)
Expand Down
Loading

0 comments on commit 83c5bb4

Please sign in to comment.