Skip to content

Commit

Permalink
fixup?
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Feb 19, 2024
1 parent 258a016 commit a16ac9b
Show file tree
Hide file tree
Showing 10 changed files with 2,006 additions and 0 deletions.
8 changes: 8 additions & 0 deletions puffin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from puffin import containers
from puffin.translate import get_namespace
from puffin.translate import to_original_object
from puffin.translate import to_polars_api

__version__ = "0.2.6"

__all__ = ["to_polars_api", "to_original_object", "get_namespace", "containers"]
102 changes: 102 additions & 0 deletions puffin/containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
try:
import polars as pl
except ModuleNotFoundError:
POLARS_AVAILABLE = False
pl = object # type: ignore[assignment]
else:
POLARS_AVAILABLE = True
try:
import pandas as pd
except ModuleNotFoundError:
PANDAS_AVAILABLE = False
pd = object
else:
PANDAS_AVAILABLE = True
try:
import cudf
except ModuleNotFoundError:
CUDF_AVAILABLE = False
cudf = object
else:
CUDF_AVAILABLE = True
try:
import modin.pandas as mpd
except ModuleNotFoundError:
MODIN_AVAILABLE = False
mpd = object
else:
MODIN_AVAILABLE = True


from typing import Any


def is_lazyframe(obj: Any) -> bool:
if hasattr(obj, "__lazyframe_namespace__"):
return True
if POLARS_AVAILABLE and isinstance(
obj, (pl.DataFrame, pl.LazyFrame, pl.Expr, pl.Series)
):
return isinstance(obj, pl.LazyFrame)
return False


def is_dataframe(obj: Any) -> bool:
if hasattr(obj, "__dataframe_namespace__"):
return True
if POLARS_AVAILABLE and isinstance(
obj, (pl.DataFrame, pl.LazyFrame, pl.Expr, pl.Series)
):
return isinstance(obj, pl.DataFrame)
return False


def is_expr(obj: Any) -> bool:
if hasattr(obj, "__expr_namespace__"):
return True
if POLARS_AVAILABLE and isinstance(
obj, (pl.DataFrame, pl.LazyFrame, pl.Expr, pl.Series)
):
return isinstance(obj, pl.Expr)
return False


def is_series(obj: Any) -> bool:
if hasattr(obj, "__series_namespace__"):
return True
if POLARS_AVAILABLE and isinstance(
obj, (pl.DataFrame, pl.LazyFrame, pl.Expr, pl.Series)
):
return isinstance(obj, pl.Series)
return False


def get_implementation(obj: Any) -> str:
if POLARS_AVAILABLE and isinstance(
obj, (pl.DataFrame, pl.LazyFrame, pl.Expr, pl.Series)
):
return "polars"
if PANDAS_AVAILABLE and isinstance(obj, (pd.DataFrame, pd.Series)):
return "pandas"
if CUDF_AVAILABLE and isinstance(obj, (cudf.DataFrame, cudf.Series)):
return "cudf"
if MODIN_AVAILABLE and isinstance(obj, mpd.DataFrame):
return "modin"
msg = f"Unknown implementation: {obj}"
raise TypeError(msg)


def is_pandas(obj: Any) -> bool:
return get_implementation(obj) == "pandas"


def is_polars(obj: Any) -> bool:
return get_implementation(obj) == "polars"


def is_cudf(obj: Any) -> bool:
return get_implementation(obj) == "cudf"


def is_modin(obj: Any) -> bool:
return get_implementation(obj) == "modin"
Loading

0 comments on commit a16ac9b

Please sign in to comment.