polars.testing.parametric.columns#

polars.testing.parametric.columns(
cols: int | Sequence[str] | None = None,
*,
dtype: OneOrMoreDataTypes | None = None,
min_cols: int | None = 0,
max_cols: int | None = 8,
unique: bool = False,
) list[column][source]#

Define multiple columns for use with the @dataframes strategy.

Generate a fixed sequence of column objects suitable for passing to the @dataframes strategy, or using standalone (note that this function is not itself a strategy).

Parameters:
cols{int, [str]}, optional

integer number of cols to create, or explicit list of column names. if omitted a random number of columns (between mincol and max_cols) are created.

dtypePolarsDataType, optional

a single dtype for all cols, or list of dtypes (the same length as cols). if omitted, each generated column is assigned a random dtype.

min_colsint, optional

if not passing an exact size, can set a minimum here (defaults to 0).

max_colsint, optional

if not passing an exact size, can set a maximum value here (defaults to MAX_COLS).

uniquebool, optional

indicate if the values generated for these columns should be unique (per-column).

Notes

Additional control is available by creating a sequence of columns explicitly, using the column class (an especially useful option is to override the default data-generating strategy for a given col/dtype).

Examples

>>> from polars.testing.parametric import columns, dataframes
>>> from hypothesis import given
>>>
>>> @given(dataframes(columns(["x", "y", "z"], unique=True)))
... def test_unique_xyz(df: pl.DataFrame) -> None:
...     assert_something(df)

Note, as ‘columns’ creates a list of native polars column definitions it can also be used independently of parametric/hypothesis tests:

>>> from string import punctuation
>>>
>>> def test_special_char_colname_init() -> None:
...     df = pl.DataFrame(schema=[(c.name, c.dtype) for c in columns(punctuation)])
...     assert len(cols) == len(df.columns)
...     assert 0 == len(df.rows())
...