Skip to content

feat: prql namespace for polars.DataFrame and polars.LazyFrame#373

Merged
eitsupi merged 7 commits intoPRQL:mainfrom
eitsupi:polars-sub-ns
Apr 29, 2024
Merged

feat: prql namespace for polars.DataFrame and polars.LazyFrame#373
eitsupi merged 7 commits intoPRQL:mainfrom
eitsupi:polars-sub-ns

Conversation

@eitsupi
Copy link
Member

@eitsupi eitsupi commented Apr 29, 2024

Close #371

import polars as pl
from datetime import date
from pyprql import polars_namespace

df = pl.DataFrame({
    "a": [1, 2, 3],
    "b": ["zz", "yy", "xx"],
    "c": [date(1999, 12, 31), date(2010, 10, 10), date(2077, 8, 8)],
})

df.prql.query("select {a, b, c_year = s'EXTRACT(year FROM c)'} | filter a > 1")
# shape: (2, 3)
# ┌─────┬─────┬────────┐
# │ a   ┆ b   ┆ c_year │
# │ --- ┆ --- ┆ ---    │
# │ i64 ┆ str ┆ i32    │
# ╞═════╪═════╪════════╡
# │ 2   ┆ yy  ┆ 2010   │
# │ 3   ┆ xx  ┆ 2077   │
# └─────┴─────┴────────┘

@eitsupi eitsupi requested a review from max-sixty April 29, 2024 02:46
@eitsupi eitsupi changed the title feat: prql sub name space for polars.DataFrame and polars.LazyFrame feat: prql sub namespace for polars.DataFrame and polars.LazyFrame Apr 29, 2024
@eitsupi eitsupi changed the title feat: prql sub namespace for polars.DataFrame and polars.LazyFrame feat: prql namespace for polars.DataFrame and polars.LazyFrame Apr 29, 2024
Copy link
Member

@max-sixty max-sixty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks cool!

No objection to starting this as an optional dependency. We can then promote it if it turns out to be used lots. (I had a bit more objection in #374 to making everything optional).

Another option is to detect whether polars is installed in pyprql.__init__ and add the namespace without having to import pyprql.polars_namespace — depends on the polars norms.

@eitsupi eitsupi marked this pull request as ready for review April 29, 2024 04:25
@eitsupi
Copy link
Member Author

eitsupi commented Apr 29, 2024

Another option is to detect whether polars is installed in pyprql.__init__ and add the namespace without having to import pyprql.polars_namespace — depends on the polars norms.

That seems like a good idea, but I don't know if it is good practice to do that here.
For now, I would like to merge and release it as is.

@eitsupi eitsupi merged commit 86a908e into PRQL:main Apr 29, 2024
@eitsupi eitsupi deleted the polars-sub-ns branch April 29, 2024 04:35
@max-sixty
Copy link
Member

For now, I would like to merge and release it as is.

For sure

That seems like a good idea, but I don't know if it is good practice to do that here.

It would be something like this in the __init__.py:

try:
    import polars
except ImportError:
    pass
else:
    # Add to namespace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prql namespace for polars.DataFrame and polars.LazyFrame

2 participants