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

feat(python): Add a show method to DataFrame and LazyFrame #19634

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7739fda
feat(python): Add show methods to DataFrame and LazyFrame
guilhem-dvr Nov 1, 2024
05a61b7
refine implementation and add tests
guilhem-dvr Nov 4, 2024
45f8fbf
remove show_in_notebook test
guilhem-dvr Nov 5, 2024
54d8da9
refactor: replace `n` with `limit`
guilhem-dvr Nov 5, 2024
25626b3
decorate existing tests with Config
guilhem-dvr Nov 6, 2024
a2a5de3
use shorthand for unlimited rows
guilhem-dvr Nov 6, 2024
da957e8
more formatting options for DataFrame.show
guilhem-dvr Nov 7, 2024
a52ef6c
mandatory OCD commit
guilhem-dvr Nov 7, 2024
5b92353
rename `LazyFrame.show` parameter to `limit`
guilhem-dvr Nov 7, 2024
973191b
add all config options to show methods
guilhem-dvr Feb 9, 2025
c525d47
remove debug print
guilhem-dvr Nov 7, 2024
1753d0c
fix ascii_tables setting
guilhem-dvr Nov 7, 2024
61c9d5a
remove auto_structify option
guilhem-dvr Nov 7, 2024
584ea3a
fix lint
guilhem-dvr Nov 7, 2024
6f13782
test incompatible settings ascii_tables & tbl_formatting
guilhem-dvr Nov 7, 2024
7680856
update docstrings with refs to config options
guilhem-dvr Nov 7, 2024
18faf2e
fix typo
guilhem-dvr Nov 7, 2024
e3bed54
fix docstring indentation issue
guilhem-dvr Nov 7, 2024
8079b87
feat: add doc entries for show methods
guilhem-dvr Nov 7, 2024
5934acb
fix bullet lists
guilhem-dvr Nov 7, 2024
9048dc9
correctly document thousands_separator type
guilhem-dvr Nov 18, 2024
095083d
set trim_decimal_zeros default to the same value as in Config
guilhem-dvr Nov 19, 2024
8710820
fix test context
guilhem-dvr Nov 19, 2024
399ddb1
test: replace config decorators with config context managers
guilhem-dvr Nov 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Descriptive
DataFrame.n_chunks
DataFrame.n_unique
DataFrame.null_count
DataFrame.show
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Descriptive
LazyFrame.describe
LazyFrame.explain
LazyFrame.show_graph
LazyFrame.show
220 changes: 219 additions & 1 deletion py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@

import polars._reexport as pl
from polars import functions as F
from polars._typing import DbWriteMode, JaxExportType, TorchExportType
from polars._typing import (
DbWriteMode,
JaxExportType,
TorchExportType,
)
from polars._utils.construction import (
arrow_to_pydf,
dataframe_to_pydf,
Expand All @@ -46,6 +50,7 @@
from polars._utils.serde import serialize_polars_object
from polars._utils.unstable import issue_unstable_warning, unstable
from polars._utils.various import (
_in_notebook,
is_bool_sequence,
no_default,
normalize_filepath,
Expand All @@ -54,6 +59,7 @@
warn_null_comparison,
)
from polars._utils.wrap import wrap_expr, wrap_ldf, wrap_s
from polars.config import Config
from polars.dataframe._html import NotebookFormatter
from polars.dataframe.group_by import DynamicGroupBy, GroupBy, RollingGroupBy
from polars.dataframe.plotting import DataFramePlot
Expand Down Expand Up @@ -134,6 +140,7 @@
CsvQuoteStyle,
DbWriteEngine,
FillNullStrategy,
FloatFmt,
FrameInitTypes,
IndexOrder,
IntoExpr,
Expand Down Expand Up @@ -165,6 +172,7 @@
UnstackDirection,
)
from polars._utils.various import NoDefault
from polars.config import TableFormatNames
from polars.interchange.dataframe import PolarsDataFrame
from polars.io.cloud import CredentialProviderFunction
from polars.ml.torch import PolarsDataset
Expand Down Expand Up @@ -11664,6 +11672,216 @@ def melt(
value_name=value_name,
)

def show(
self,
limit: int | None = 5,
*,
ascii_tables: bool | None = None,
decimal_separator: str | None = None,
thousands_separator: str | bool | None = None,
float_precision: int | None = None,
fmt_float: FloatFmt | None = None,
fmt_str_lengths: int | None = None,
fmt_table_cell_list_len: int | None = None,
tbl_cell_alignment: Literal["LEFT", "CENTER", "RIGHT"] | None = None,
tbl_cell_numeric_alignment: Literal["LEFT", "CENTER", "RIGHT"] | None = None,
tbl_cols: int | None = None,
tbl_column_data_type_inline: bool | None = None,
tbl_dataframe_shape_below: bool | None = None,
tbl_formatting: TableFormatNames | None = None,
tbl_hide_column_data_types: bool | None = None,
tbl_hide_column_names: bool | None = None,
tbl_hide_dtype_separator: bool | None = None,
tbl_hide_dataframe_shape: bool | None = None,
tbl_width_chars: int | None = None,
trim_decimal_zeros: bool | None = True,
) -> None:
"""
Show the first `n` rows.

Parameters
----------
limit : int
Numbers of rows to show. If a negative value is passed, return all rows
except the last `abs(n)`. If None is passed, return all rows.
ascii_tables : bool
Use ASCII characters to display table outlines. Set False to revert to the
default UTF8_FULL_CONDENSED formatting style. See
:func:`Config.set_ascii_tables` for more information.
decimal_separator : str
Set the decimal separator character. See
:func:`Config.set_decimal_separator` for more information.
thousands_separator : str, bool
Set the thousands grouping separator character. See
:func:`Config.set_thousands_separator` for more information.
float_precision : int
Number of decimal places to display for floating point values. See
:func:`Config.set_float_precision` for more information.
fmt_float : {"mixed", "full"}
Control how floating point values are displayed. See
:func:`Config.set_fmt_float` for more information. Supported options are:

* "mixed": Limit the number of decimal places and use scientific notation
for large/small values.
* "full": Print the full precision of the floating point number.

fmt_str_lengths : int
Number of characters to display for string values. See
:func:`Config.set_fmt_str_lengths` for more information.
fmt_table_cell_list_len : int
Number of elements to display for List values. See
:func:`Config.set_fmt_table_cell_list_len` for more information.
tbl_cell_alignment : str
Set table cell alignment. See :func:`Config.set_tbl_cell_alignment` for more
information. Supported options are:

* "LEFT": left aligned
* "CENTER": center aligned
* "RIGHT": right aligned

tbl_cell_numeric_alignment : str
Set table cell alignment for numeric columns. See
:func:`Config.set_tbl_cell_numeric_alignment` for more information.
Supported options are:

* "LEFT": left aligned
* "CENTER": center aligned
* "RIGHT": right aligned

tbl_cols : int
Number of columns to display. See :func:`Config.set_tbl_cols` for more
information.
tbl_column_data_type_inline : bool
Moves the data type inline with the column name (to the right, in
parentheses). See :func:`Config.set_tbl_column_data_type_inline` for more
information.
tbl_dataframe_shape_below : bool
Print the DataFrame shape information below the data when displaying tables.
See :func:`Config.set_tbl_dataframe_shape_below` for more information.
tbl_formatting : str
Set table formatting style. See :func:`Config.set_tbl_formatting` for more
information. Supported options are:

* "ASCII_FULL": ASCII, with all borders and lines, including row dividers.
* "ASCII_FULL_CONDENSED": Same as ASCII_FULL, but with dense row spacing.
* "ASCII_NO_BORDERS": ASCII, no borders.
* "ASCII_BORDERS_ONLY": ASCII, borders only.
* "ASCII_BORDERS_ONLY_CONDENSED": ASCII, borders only, dense row spacing.
* "ASCII_HORIZONTAL_ONLY": ASCII, horizontal lines only.
* "ASCII_MARKDOWN": Markdown format (ascii ellipses for truncated values).
* "MARKDOWN": Markdown format (utf8 ellipses for truncated values).
* "UTF8_FULL": UTF8, with all borders and lines, including row dividers.
* "UTF8_FULL_CONDENSED": Same as UTF8_FULL, but with dense row spacing.
* "UTF8_NO_BORDERS": UTF8, no borders.
* "UTF8_BORDERS_ONLY": UTF8, borders only.
* "UTF8_HORIZONTAL_ONLY": UTF8, horizontal lines only.
* "NOTHING": No borders or other lines.

tbl_hide_column_data_types : bool
Hide table column data types (i64, f64, str etc.). See
:func:`Config.set_tbl_hide_column_data_types` for more information.
tbl_hide_column_names : bool
Hide table column names. See :func:`Config.set_tbl_hide_column_names` for
more information.
tbl_hide_dtype_separator : bool
Hide the '---' separator between the column names and column types. See
:func:`Config.set_tbl_hide_dtype_separator` for more information.
tbl_hide_dataframe_shape : bool
Hide the DataFrame shape information when displaying tables. See
:func:`Config.set_tbl_hide_dataframe_shape` for more information.
tbl_width_chars : int
Set the maximum width of a table in characters. See
:func:`Config.set_tbl_width_chars` for more information.
trim_decimal_zeros : bool
Strip trailing zeros from Decimal data type values. See
:func:`Config.set_trim_decimal_zeros` for more information.

See Also
--------
head

Examples
--------
>>> df = pl.DataFrame(
... {
... "foo": [1, 2, 3, 4, 5],
... "bar": [6, 7, 8, 9, 10],
... "ham": ["a", "b", "c", "d", "e"],
... }
... )
>>> df.show(3)
shape: (3, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ ham │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ str │
╞═════╪═════╪═════╡
│ 1 ┆ 6 ┆ a │
│ 2 ┆ 7 ┆ b │
│ 3 ┆ 8 ┆ c │
└─────┴─────┴─────┘

Pass a negative value to get all rows `except` the last `abs(n)`.

>>> df.show(-3)
shape: (2, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ ham │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ str │
╞═════╪═════╪═════╡
│ 1 ┆ 6 ┆ a │
│ 2 ┆ 7 ┆ b │
└─────┴─────┴─────┘
"""
if limit is None:
df = self
tbl_rows = -1
else:
df = self.head(limit)
if limit < 0:
tbl_rows = self.height - abs(limit)
else:
tbl_rows = limit

if ascii_tables is not None and tbl_formatting is not None:
msg = "Can not set `ascii_tables` and `tbl_formatting` at the same time."
raise ValueError(msg)
if ascii_tables is not None:
if ascii_tables:
tbl_formatting = "ASCII_FULL_CONDENSED"
else:
tbl_formatting = "UTF8_FULL_CONDENSED"

with Config(
ascii_tables=ascii_tables,
decimal_separator=decimal_separator,
thousands_separator=thousands_separator,
float_precision=float_precision,
fmt_float=fmt_float,
fmt_str_lengths=fmt_str_lengths,
fmt_table_cell_list_len=fmt_table_cell_list_len,
tbl_cell_alignment=tbl_cell_alignment,
tbl_cell_numeric_alignment=tbl_cell_numeric_alignment,
tbl_cols=tbl_cols,
tbl_column_data_type_inline=tbl_column_data_type_inline,
tbl_dataframe_shape_below=tbl_dataframe_shape_below,
tbl_formatting=tbl_formatting,
tbl_hide_column_data_types=tbl_hide_column_data_types,
tbl_hide_column_names=tbl_hide_column_names,
tbl_hide_dtype_separator=tbl_hide_dtype_separator,
tbl_hide_dataframe_shape=tbl_hide_dataframe_shape,
tbl_rows=tbl_rows,
tbl_width_chars=tbl_width_chars,
trim_decimal_zeros=trim_decimal_zeros,
):
if _in_notebook():
from IPython.display import display_html

display_html(df)
else:
print(df)

def _to_metadata(
self,
columns: None | str | list[str] = None,
Expand Down
Loading