panel.util.dataframe module#

Backend neutral helpers for tabular data.

Panel’s data components historically assume pandas. pandas-like frames (pandas, modin, cuDF) carry a row index which Panel uses to identify rows across filtering, sorting and editing. Other DataFrame libraries supported by Narwhals, e.g. Polars and PyArrow, have no such index, so for those Panel identifies a row by its position in the original frame and tracks it through filtering and sorting using a temporary row index column (see ROW_INDEX).

Write operations never mutate these frames in place, since some backends (e.g. PyArrow) are immutable. Instead a new frame is built and the component’s value is replaced.

Narwhals is imported inside the functions to avoid adding to the import time of Panel.

panel.util.dataframe.add_row_index(obj: Any, offset: int = 0) → Any[source]#

Adds a column recording the position of each row, so rows can be tracked through filtering and sorting.

panel.util.dataframe.all_of(exprs: Sequence[nw.Expr]) → nw.Expr[source]#

Combines filter expressions, treating missing values as not matching the filter.

panel.util.dataframe.column_names(obj: Any) → list[Any][source]#

Returns the column names of a dictionary of columns or DataFrame.

panel.util.dataframe.copy_data(obj: Any) → Any[source]#

Copies data which may be mutated in place, i.e. dictionaries and pandas-like DataFrames. Other DataFrames are never mutated so they are returned as is.

panel.util.dataframe.dtype_kind(dtype: nw.dtypes.DType) → str[source]#

Maps a Narwhals dtype onto the equivalent NumPy dtype kind.

panel.util.dataframe.evaluate_mask(obj: t.Any, expr: nw.Expr) → np.ndarray[source]#

Evaluates a filter expression on a frame into a boolean mask.

panel.util.dataframe.filter_expr(column: str, value: t.Any) → nw.Expr | None[source]#

Returns an expression filtering a column by a value, where the value is a scalar (equality), a list or set (membership) or a tuple (inclusive range). Returns None if the value does not filter.

panel.util.dataframe.frame_from_columns(data: Mapping[str, Any], template: Any) → Any[source]#

Builds a frame from a dictionary of columns using the backend of the template frame.

panel.util.dataframe.has_index(obj: Any) → bool[source]#

Whether the object is a pandas-like DataFrame, which carries a row index that can label rows.

panel.util.dataframe.header_filter_exprs(obj: t.Any, filters: Iterable[Mapping[str, t.Any]], filter_defs: Mapping[str, t.Any] | None = None) → list[nw.Expr][source]#

Returns expressions implementing Tabulator header filter specifications, each declaring a ‘field’, ‘type’ and ‘value’. The frame is only used to look up the column dtypes.

panel.util.dataframe.indexed_column_data(df: Any) → dict[str, ndarray][source]#

Converts a pandas-like DataFrame into a dictionary of NumPy arrays, including a column for its index, which can be sent to a ColumnDataSource. Produces the same columns as bokeh’s ColumnDataSource.from_df, without its frame copy and reset_index for pandas DataFrames.

panel.util.dataframe.is_dataframe(obj: Any) → bool[source]#

Whether the object is an eager DataFrame from any library supported by Narwhals, e.g. pandas, Polars or PyArrow.

panel.util.dataframe.is_indexless(obj: Any) → bool[source]#

Whether the object is a DataFrame without a row index, e.g. a Polars DataFrame or a PyArrow Table.

panel.util.dataframe.row_dict(obj: Any, row: int) → dict[str, Any][source]#

Returns a row as a dictionary of column name to value, omitting the row index column.

panel.util.dataframe.row_positions(obj: Any) → list[int][source]#

Returns the positions of the rows in the original frame, as recorded by add_row_index.

panel.util.dataframe.set_values(obj: Any, column: str, rows: Sequence[int] | ndarray, values: Any) → Any[source]#

Returns a new frame with the values of the column at the supplied row positions replaced.

panel.util.dataframe.sort_frame(obj: Any, fields: Sequence[str], ascending: Sequence[bool]) → Any[source]#

Sorts a frame mirroring the sorting algorithm of Tabulator, i.e. strings are compared case-insensitively with missing strings treated as empty, other missing values are placed last and ties keep their original order, like Tabulator’s numeric _index tie-breaker.

panel.util.dataframe.to_column_data(obj: Any) → dict[str, ndarray][source]#

Converts a DataFrame into a dictionary of NumPy arrays, which can be sent to a ColumnDataSource.

panel.util.dataframe.to_narwhals(obj: t.Any) → nw.DataFrame[t.Any][source]#

Wraps a native DataFrame in a Narwhals DataFrame without copying.