Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Adding Narwhals as compatibility layer between libraries #339

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
"Programming Language :: Python :: 3.13",
]
requires-python = ">= 3.7"
dependencies = ["IPython", "pandas", "numpy"]
dependencies = ["IPython", "pandas", "numpy", "narwhals"]
dynamic = ["version"]

[project.optional-dependencies]
Expand Down
27 changes: 15 additions & 12 deletions src/itables/datatables_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import re
import warnings

import narwhals.stable.v1 as nw
import numpy as np
import pandas as pd
import pandas.io.formats.format as fmt
from narwhals.stable.v1.dependencies import is_pandas_dataframe

try:
import polars as pl
Expand Down Expand Up @@ -83,15 +85,16 @@ def datatables_rows(df, count=None, warn_on_unexpected_types=False, pure_json=Fa
"""Format the values in the table and return the data, row by row, as requested by DataTables"""
# We iterate over columns using an index rather than the column name
# to avoid an issue in case of duplicated column names #89
if count is None or len(df.columns) == count:
empty_columns = []
else:
# When the header requires more columns (#141), we append empty columns on the left
missing_columns = count - len(df.columns)
assert missing_columns > 0
empty_columns = [[None] * len(df)] * missing_columns
if is_pandas_dataframe(df):

if count is None or len(df.columns) == count:
empty_columns = []
else:
# When the header requires more columns (#141), we append empty columns on the left
missing_columns = count - len(df.columns)
assert missing_columns > 0
empty_columns = [[None] * len(df)] * missing_columns

try:
# Pandas DataFrame
data = list(
zip(
Expand All @@ -108,17 +111,17 @@ def datatables_rows(df, count=None, warn_on_unexpected_types=False, pure_json=Fa
cls=generate_encoder(warn_on_unexpected_types),
allow_nan=not pure_json,
)
except AttributeError:
else:
# Polars DataFrame
df = nw.from_native(df)

Choose a reason for hiding this comment

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

we should probably use eager_only=True if we're calling rows

data = list(df.iter_rows())

Choose a reason for hiding this comment

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

drive-by - this would be more efficient as data = df.rows() (it produces the same thing, just faster)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did the PR for this. Thanks for the feedback.

import polars as pl

has_bigints = any(
(
x.dtype == pl.Int64
x.dtype == nw.Int64
and ((x > JS_MAX_SAFE_INTEGER).any() or (x < JS_MIN_SAFE_INTEGER).any())
)
or (x.dtype == pl.UInt64 and (x > JS_MAX_SAFE_INTEGER).any())
or (x.dtype == nw.UInt64 and (x > JS_MAX_SAFE_INTEGER).any())
for x in (df[col] for col in df.columns)
)
js = json.dumps(data, cls=generate_encoder(False), allow_nan=not pure_json)
Expand Down
Loading