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 show methods to DataFrame and LazyFrame #19634

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
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
214 changes: 214 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 @@ -63,6 +64,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 @@ -147,6 +149,7 @@
CsvQuoteStyle,
DbWriteEngine,
FillNullStrategy,
FloatFmt,
FrameInitTypes,
IndexOrder,
IntoExpr,
Expand Down Expand Up @@ -176,6 +179,7 @@
UnstackDirection,
)
from polars._utils.various import NoDefault
from polars.config import TableFormatNames
from polars.interchange.dataframe import PolarsDataFrame
from polars.ml.torch import PolarsDataset

Expand Down Expand Up @@ -11263,6 +11267,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 = None,
) -> 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
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
Loading