From d9d03d21cb4b8164a673accb5c77cfc0756999fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Fri, 29 Nov 2024 15:33:31 +0100 Subject: [PATCH 1/3] wip --- src/itables/datatables_format.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/itables/datatables_format.py b/src/itables/datatables_format.py index 5fb3b861..5825f284 100644 --- a/src/itables/datatables_format.py +++ b/src/itables/datatables_format.py @@ -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 @@ -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( @@ -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) data = list(df.iter_rows()) - 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) From 14b8d22139fe00cf2e3af3aa891b464d7dae03e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Fri, 29 Nov 2024 18:35:17 +0100 Subject: [PATCH 2/3] Adding Narwhals --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index fa2d7323..ae5c7300 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] From 761ab12ada25e6f2411c5cbb1aede3ece414df5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Wed, 4 Dec 2024 12:05:45 +0100 Subject: [PATCH 3/3] added eager_only=True --- README.md | 1 + docs/quick_start.md | 1 + src/itables/datatables_format.py | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b68df60b..ef01a9ce 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/docs/quick_start.md b/docs/quick_start.md index cee25d9c..6302121d 100644 --- a/docs/quick_start.md +++ b/docs/quick_start.md @@ -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) diff --git a/src/itables/datatables_format.py b/src/itables/datatables_format.py index 5825f284..7a184bd7 100644 --- a/src/itables/datatables_format.py +++ b/src/itables/datatables_format.py @@ -113,8 +113,8 @@ def datatables_rows(df, count=None, warn_on_unexpected_types=False, pure_json=Fa ) else: # Polars DataFrame - df = nw.from_native(df) - data = list(df.iter_rows()) + df = nw.from_native(df, eager_only=True) + data = df.rows() has_bigints = any( (