Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d7c788

Browse files
author
eugene
authored
feat(formatters): Add consistent Series formatting for pandas and polars (marimo-team#3232)
1 parent e614b80 commit 6d7c788

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

marimo/_output/formatters/df_formatters.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ def _show_marimo_dataframe(
4343
LOGGER.warning("Failed to format DataFrame: %s", e)
4444
return ("text/html", df._repr_html_())
4545

46+
@formatting.opinionated_formatter(pl.Series)
47+
def _show_marimo_series(
48+
series: pl.Series,
49+
) -> tuple[KnownMimeType, str]:
50+
try:
51+
# Table need a column name for operations
52+
if series.name is None or series.name == "":
53+
df = pl.DataFrame({"value": series})
54+
else:
55+
df = series.to_frame()
56+
return table(df, selection=None, pagination=True)._mime_()
57+
except Exception as e:
58+
LOGGER.warning("Failed to format Series: %s", e)
59+
return ("text/html", series._repr_html_())
60+
4661

4762
class PyArrowFormatter(FormatterFactory):
4863
@staticmethod

marimo/_output/formatters/pandas_formatters.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
from typing import Any
55

6+
from marimo._loggers import marimo_logger
67
from marimo._messaging.mimetypes import KnownMimeType
78
from marimo._output.formatters.df_formatters import include_opinionated
89
from marimo._output.formatters.formatter_factory import FormatterFactory
910
from marimo._output.utils import flatten_string
1011
from marimo._plugins.ui._impl.table import table
1112

13+
LOGGER = marimo_logger()
14+
1215

1316
class PandasFormatter(FormatterFactory):
1417
@staticmethod
@@ -32,6 +35,21 @@ def _show_marimo_dataframe(
3235
) -> tuple[KnownMimeType, str]:
3336
return table(df, selection=None, pagination=True)._mime_()
3437

38+
@formatting.opinionated_formatter(pd.Series)
39+
def _show_marimo_series(
40+
series: pd.Series[Any],
41+
) -> tuple[KnownMimeType, str]:
42+
try:
43+
# Table need a column name for operations
44+
if series.name is None:
45+
series = series.rename("value")
46+
return table(
47+
series.to_frame(), selection=None, pagination=True
48+
)._mime_()
49+
except Exception as e:
50+
LOGGER.warning("Failed to format Series: %s", e)
51+
return ("text/html", series._repr_html_())
52+
3553
@formatting.formatter(pd.DataFrame)
3654
def _show_dataframe(df: pd.DataFrame) -> tuple[KnownMimeType, str]:
3755
max_rows = pd.get_option("display.max_rows")
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "marimo",
5+
# "pandas==2.2.3",
6+
# "polars==1.17.1",
7+
# ]
8+
# ///
9+
10+
import marimo
11+
12+
__generated_with = "0.10.5"
13+
app = marimo.App(width="medium")
14+
15+
16+
@app.cell
17+
def _():
18+
import marimo as mo
19+
import pandas as pd
20+
import polars as pl
21+
return mo, pd, pl
22+
23+
24+
@app.cell
25+
def _(mo):
26+
num_range = mo.ui.slider(start=10, step=1, stop=100)
27+
num_range
28+
return (num_range,)
29+
30+
31+
@app.cell
32+
def _(num_range, pd):
33+
# Pandas Series with name
34+
pd_named = pd.Series(list(range(num_range.value)), name="numbers")
35+
pd_named
36+
return (pd_named,)
37+
38+
39+
@app.cell
40+
def _(num_range, pd):
41+
# Pandas Series without name
42+
pd_unnamed = pd.Series(list(range(num_range.value)))
43+
pd_unnamed
44+
return (pd_unnamed,)
45+
46+
47+
@app.cell
48+
def _(num_range, pl):
49+
# Polars Series with name
50+
pl_named = pl.Series("numbers", list(range(num_range.value)))
51+
pl_named
52+
return (pl_named,)
53+
54+
55+
@app.cell
56+
def _(num_range, pl):
57+
# Polars Series without name
58+
pl_unnamed = pl.Series(list(range(num_range.value)))
59+
pl_unnamed
60+
return (pl_unnamed,)
61+
62+
63+
@app.cell
64+
def _(num_range, pl):
65+
# Polars Series with empty string name
66+
pl_empty_name = pl.Series("", list(range(num_range.value)))
67+
pl_empty_name
68+
return (pl_empty_name,)
69+
70+
71+
if __name__ == "__main__":
72+
app.run()

0 commit comments

Comments
 (0)