Polars DataFrame#
IMPORT / EXPORT to and from polars.DataFrame#
"""
HTTP Transport
EXPORT and IMPORT from Exasol to Polars DataFrames
Make sure to enable compression for Wi-Fi connections to improve performance
"""
import examples._config as config
import pyexasol
def connect() -> pyexasol.ExaConnection:
# Connect with compression enabled
return pyexasol.connect(
dsn=config.dsn,
user=config.user,
password=config.password,
schema=config.schema,
compression=True,
websocket_sslopt=config.websocket_sslopt,
)
# Prepare empty table
with connect() as C:
C.execute("TRUNCATE TABLE users_copy")
# Export from Exasol table into polars.DataFrame
with connect() as C:
df = C.export_to_polars("users")
statement = C.last_statement()
print(df.describe())
print(f"EXPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Import from polars DataFrame into Exasol table
with connect() as C:
C.import_from_polars(df, "users_copy")
statement = C.last_statement()
print(f"IMPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Alternatively use polars LazyFrame the same way (this appends to the rows imported above)
with connect() as C:
lf = df.lazy()
C.import_from_polars(lf, "users_copy")
statement = C.last_statement()
print(f"IMPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Export from SQL query
with connect() as C:
df = C.export_to_polars(
"SELECT user_id, user_name FROM users WHERE user_id >= 5000"
)
statement = C.last_statement()
print(df.describe())
print(f"EXPORTED {statement.rowcount()} rows in {statement.execution_time}s")