-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zkeram
committed
Jan 31, 2025
1 parent
0c47746
commit 0446827
Showing
2 changed files
with
125 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import datafusion | ||
import pandas as pd | ||
import polars as pl | ||
from typing_extensions import TYPE_CHECKING, Union | ||
from pathlib import Path | ||
|
||
def get_py_ctx() -> datafusion.context.SessionContext: | ||
return datafusion.context.SessionContext() | ||
|
||
def read_df_to_datafusion( | ||
py_ctx: datafusion.context.SessionContext, | ||
df: Union[str, pl.DataFrame, pl.LazyFrame, pd.DataFrame] | ||
) -> datafusion.dataframe: | ||
if isinstance(df, pl.DataFrame): | ||
return py_ctx.from_polars(df) | ||
elif isinstance(df, pd.DataFrame): | ||
return py_ctx.from_pandas(df) | ||
elif isinstance(df, pl.LazyFrame): | ||
return py_ctx.from_polars(df.collect()) | ||
elif isinstance(df, str): | ||
ext = Path(df).suffix | ||
if ext == '.csv': | ||
return py_ctx.read_csv(df) | ||
elif ext == '.bed': | ||
return py_ctx.read_csv(df, has_header=False, delimited='\t', file_extension='.bed', schema=pa.schema([ | ||
(DEFAULT_COLUMNS[0], pa.string()), | ||
(DEFAULT_COLUMNS[1], pa.int64()), | ||
(DEFAULT_COLUMNS[2], pa.int64())])) | ||
else: | ||
return py_ctx.read_parquet(df) | ||
raise ValueError("Invalid `df` argument.") | ||
|
||
def df_to_lazyframe( | ||
df: datafusion.DataFrame | ||
) -> pl.LazyFrame: | ||
# TODO: make it actually lazy | ||
''' | ||
def _get_lazy( | ||
with_columns: list[str] | None, | ||
predicate: pl.Expr | None, | ||
n_rows: int | None, | ||
batch_size: int | None | ||
) -> Iterator[pl.DataFrame]: | ||
return register_io_source(_overlap_source, schema=schema) | ||
''' | ||
return df.to_polars().lazy() | ||
|
||
def convert_result( | ||
df: datafusion.DataFrame, | ||
output_type: str, | ||
streaming: bool | ||
) -> Union[pl.LazyFrame, pl.DataFrame, pd.DataFrame]: | ||
# TODO: implement streaming | ||
if streaming == True: | ||
#raise NotImplementedError("streaming is not implemented") | ||
return df.to_polars().lazy() | ||
if output_type == "polars.DataFrame": | ||
return df.to_polars() | ||
elif output_type == "pandas.DataFrame": | ||
return df.to_pandas() | ||
elif output_type == "polars.LazyFrame": | ||
return df_to_lazyframe(df) | ||
raise ValueError("Invalid `output_type` argument") |
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