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 all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![CI](https://github.com/mwouts/itables/actions/workflows/continuous-integration.yml/badge.svg?branch=main)](https://github.com/mwouts/itables/actions)
[![codecov.io](https://codecov.io/github/mwouts/itables/coverage.svg?branch=main)](https://codecov.io/github/mwouts/itables?branch=main)
[![MIT License](https://img.shields.io/github/license/mwouts/itables)](LICENSE)
[![Pypi](https://img.shields.io/pypi/v/itables.svg)](https://pypi.python.org/pypi/itables)
[![Conda Version](https://img.shields.io/conda/vn/conda-forge/itables.svg)](https://anaconda.org/conda-forge/itables)
[![pyversions](https://img.shields.io/pypi/pyversions/itables.svg)](https://pypi.python.org/pypi/itables)
Expand Down
1 change: 1 addition & 0 deletions docs/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ kernelspec:

[![CI](https://github.com/mwouts/itables/actions/workflows/continuous-integration.yml/badge.svg?branch=main)](https://github.com/mwouts/itables/actions)
[![codecov.io](https://codecov.io/github/mwouts/itables/coverage.svg?branch=main)](https://codecov.io/github/mwouts/itables?branch=main)
[![MIT License](https://img.shields.io/github/license/mwouts/itables)](https://github.com/mwouts/itables/blob/main/LICENSE)
[![Pypi](https://img.shields.io/pypi/v/itables.svg)](https://pypi.python.org/pypi/itables)
[![Conda Version](https://img.shields.io/conda/vn/conda-forge/itables.svg)](https://anaconda.org/conda-forge/itables)
[![pyversions](https://img.shields.io/pypi/pyversions/itables.svg)](https://pypi.python.org/pypi/itables)
Expand Down
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
29 changes: 16 additions & 13 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
data = list(df.iter_rows())
import polars as pl
df = nw.from_native(df, eager_only=True)
data = df.rows()

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