-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
prql
namespace for polars.DataFrame and polars.LazyFrame (#373)
- Loading branch information
Showing
7 changed files
with
77 additions
and
11 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -2,5 +2,3 @@ | |
|
||
import prqlc # noqa: F401 | ||
from prqlc import compile # noqa: F401 | ||
|
||
__version__ = "0.8.0" |
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 @@ | ||
from .prql import PrqlNamespace # noqa: F401 |
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,25 @@ | ||
from __future__ import annotations | ||
import polars as pl | ||
import prqlc | ||
|
||
from typing import TypeVar, Generic | ||
|
||
|
||
T_DF = TypeVar("T_DF", pl.DataFrame, pl.LazyFrame) | ||
|
||
|
||
@pl.api.register_dataframe_namespace("prql") | ||
@pl.api.register_lazyframe_namespace("prql") | ||
class PrqlNamespace(Generic[T_DF]): | ||
def __init__(self, df: T_DF): | ||
self._df = df | ||
|
||
def query(self, prql_query: str, *, table_name: str | None = None) -> T_DF: | ||
prepended_query: str = f"from self \n {prql_query}" | ||
sql_query: str = prqlc.compile( | ||
prepended_query, | ||
options=prqlc.CompileOptions( | ||
target="sql.any", format=False, signature_comment=False | ||
), | ||
) | ||
return self._df.sql(sql_query, table_name=table_name) |
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,29 @@ | ||
import polars as pl | ||
import pytest | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def import_accessor(): | ||
import pyprql.polars_namespace # noqa | ||
|
||
|
||
def test_polars_df_namespace(): | ||
df = pl.DataFrame({"latitude": [1, 2, 3], "longitude": [1, 2, 3]}) | ||
res = df.prql.query( | ||
"select {latitude, longitude} | filter latitude > 1 | sort latitude" | ||
) | ||
assert res.to_dict(as_series=False) == { | ||
"latitude": [2, 3], | ||
"longitude": [2, 3], | ||
} | ||
|
||
|
||
def test_polars_lf_namespace(): | ||
df = pl.LazyFrame({"latitude": [1, 2, 3], "longitude": [1, 2, 3]}) | ||
res = df.prql.query( | ||
"select {latitude, longitude} | filter latitude > 1 | sort latitude" | ||
).collect() | ||
assert res.to_dict(as_series=False) == { | ||
"latitude": [2, 3], | ||
"longitude": [2, 3], | ||
} |