Data frame & array assertions¶
Equality assertions for pandas / polars data frames and numpy arrays (requires the data extra).
Fluent assertions for pandas/polars frames and numpy arrays (optional [data] extra).
is_frame_equal ¶
Asserts that a pandas/polars DataFrame or Series equals expected.
Delegates to the owning library's own assert_frame_equal / assert_series_equal, so all
comparison semantics (dtype strictness, row/column order, tolerance, categoricals, ...) are the
library's. Any keyword options are passed straight through.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
object
|
the expected frame/series (same library as val) |
required |
**options
|
Any
|
keyword options forwarded to the library's |
{}
|
Examples:
Usage:
import pandas as pd
assert_that(pd.DataFrame({"a": [1, 2]})).is_frame_equal(pd.DataFrame({"a": [1, 2]}))
assert_that(actual).is_frame_equal(expected, check_dtype=False)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the frames/series are not equal (carrying the library's own diff message) |
TypeError
|
if val is not a pandas or polars |
ImportError
|
if the owning library is not installed |
Source code in assertpy2/dataframe.py
is_array_equal ¶
Asserts that val equals expected element-wise, via numpy's assert_array_equal.
Works on any array-likes numpy can coerce (ndarray, nested lists, ...); shape and every
element must match exactly (with NaN treated as equal, per numpy).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
object
|
the expected array-like |
required |
Examples:
Usage:
import numpy as np
assert_that(np.array([1, 2, 3])).is_array_equal(np.array([1, 2, 3]))
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the arrays are not equal (carrying numpy's own diff message) |
ImportError
|
if numpy is not installed |
Source code in assertpy2/dataframe.py
is_array_close_to ¶
is_array_close_to(
expected: object,
*,
rtol: float = 1e-05,
atol: float = 1e-08,
equal_nan: bool = False,
) -> Self
Asserts that val is element-wise close to expected, via numpy's assert_allclose.
The float-tolerant counterpart to is_array_equal(),
for comparing computed arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
object
|
the expected array-like |
required |
rtol
|
float
|
relative tolerance (numpy default |
1e-05
|
atol
|
float
|
absolute tolerance (numpy default |
1e-08
|
equal_nan
|
bool
|
whether |
False
|
Examples:
Usage:
import numpy as np
assert_that(np.array([1.0, 2.0])).is_array_close_to(np.array([1.0, 2.0000001]))
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the arrays are not close (carrying numpy's own diff message) |
ImportError
|
if numpy is not installed |