-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from abstractqqq/compat
- Loading branch information
Showing
33 changed files
with
818 additions
and
512 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
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,3 @@ | ||
## Compatibility with other DataFrames | ||
|
||
::: polars_ds.compat |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Compatibility with other Dataframes. | ||
This module provides compatibility with other dataframe libraries that: | ||
1. Have a notion of Series | ||
2. The Series implements the array protocal, which means it can be translated to NumPy array via | ||
.__array__() method. | ||
Since most dataframe libraries can turn their Series into NumPy (or vice versa) with 0 copy, | ||
this compatibility layer has very little overhead. The only constraint is that the dataframe | ||
must be eager, in the sense that data is already loaded in memory. The reason for this is that | ||
the notion of a Series doesn't really exist in the lazy world, and lazy columns cannot be turned | ||
to NumPy arrays. | ||
When using this compatibility, the output is always a Polars Series. This is because the output | ||
type could be Polars struct/list Series, which are Polars-specific types. It is up to the user | ||
what to do with the output. | ||
For example, in order to use PDS with Pandas dataframe, say df:pd.DataFrame, one needs to write | ||
>>> from polars_ds.compat import compat as pds2 | ||
>>> # Output is a Polars Series. | ||
>>> pds2.query_roc_auc(df_pd["actual"], df_pd["predicted"]) | ||
>>> # For more advanced queries | ||
>>> pds2.lin_reg( | ||
>>> df["x1"], df["x2"], df["x3"] | ||
>>> target = df["y"], | ||
>>> return_pred = True | ||
>>> ) | ||
Question: if output is still Polars, then the user must still use both Polars and Pandas. | ||
Why bother with compatibility? | ||
Here are some answers I consider to be true (or self-promotion :)) | ||
1. PDS is a very light weight package that can reduce dependencies in your project. | ||
2. For projects with mixed dataframes, it is sometimes not a good idea to cast the | ||
entire Pandas (or other) dataframe to Polars. | ||
3. Some PDS functions are faster than SciPy / Sklearn equivalents. | ||
4. For ad-hoc analysis that involves say something like linear regression, PDS is easier to | ||
use than other package. | ||
""" | ||
|
||
from ._compat import compat | ||
|
||
import warnings | ||
warnings.warn( | ||
"The compatibility layer is considered experimental.", | ||
stacklevel=2 | ||
) |
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,63 @@ | ||
import polars as pl | ||
import numpy as np | ||
from typing import Any, Callable | ||
import polars_ds as pds | ||
|
||
# Everything in __init__.py of polars_ds that this shouldn't be able to call | ||
CANNOT_CALL = { | ||
"frame", | ||
"str_to_expr", | ||
"pl", | ||
"annotations", | ||
"__version__", | ||
} | ||
|
||
__all__ = ["compat"] | ||
|
||
class _Compat(): | ||
|
||
@staticmethod | ||
def _try_into_series(x:Any, name:str) -> Any: | ||
""" | ||
Try to map the input to a Polars Series by going through a NumPy array. If | ||
this is not possible, return the original input. | ||
""" | ||
if isinstance(x, np.ndarray): | ||
return pl.lit(pl.Series(name=name, values=x)) | ||
elif isinstance(x, pl.Series): | ||
return pl.lit(x) | ||
elif hasattr(x, "__array__"): | ||
return pl.lit(pl.Series(name=name, values=x.__array__())) | ||
else: | ||
return x | ||
|
||
def __getattr__(self, name:str) -> pl.Series: | ||
if name in CANNOT_CALL: | ||
raise ValueError(f"`{name}` exists but doesn't work in compat mode.") | ||
|
||
func = getattr(pds, name) | ||
def compat_wrapper(*args, **kwargs) -> Callable: | ||
positionals = list(args) | ||
if len(positionals) <= 0: | ||
raise ValueError("There must be at least 1 positional argument!") | ||
|
||
new_args = ( | ||
_Compat._try_into_series(x, name = str(i)) | ||
for i, x in enumerate(positionals) | ||
) | ||
new_kwargs = { | ||
n: _Compat._try_into_series(v, name = n) | ||
for n, v in kwargs.items() | ||
} | ||
# An eager df, drop output col, so a pl.Series | ||
return ( | ||
pl.select( | ||
func(*new_args, **new_kwargs).alias("__output__") | ||
).drop_in_place("__output__") | ||
.rename(name.replace("query_", "")) | ||
) | ||
|
||
return compat_wrapper | ||
|
||
compat: _Compat = _Compat() | ||
|
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
Oops, something went wrong.