narwhals.testing
assert_series_equal
assert_series_equal(
left: Series[IntoSeriesT],
right: Series[IntoSeriesT],
*,
check_dtypes: bool = True,
check_names: bool = True,
check_order: bool = True,
check_exact: bool = False,
rel_tol: float = 1e-05,
abs_tol: float = 1e-08,
categorical_as_str: bool = False
) -> None
Assert that the left and right Series are equal.
Raises a detailed AssertionError
if the Series differ.
This function is intended for use in unit tests.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
left
|
Series[IntoSeriesT]
|
The first Series to compare. |
required |
right
|
Series[IntoSeriesT]
|
The second Series to compare. |
required |
check_dtypes
|
bool
|
Requires data types to match. |
True
|
check_names
|
bool
|
Requires names to match. |
True
|
check_order
|
bool
|
Requires elements to appear in the same order. |
True
|
check_exact
|
bool
|
Requires float values to match exactly. If set to |
False
|
rel_tol
|
float
|
Relative tolerance for inexact checking, given as a fraction of the
values in |
1e-05
|
abs_tol
|
float
|
Absolute tolerance for inexact checking. |
1e-08
|
categorical_as_str
|
bool
|
Cast categorical columns to string before comparing. Enabling this helps compare columns that do not share the same string cache. |
False
|
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>> from narwhals.testing import assert_series_equal
>>> s1 = nw.from_native(pd.Series([1, 2, 3]), series_only=True)
>>> s2 = nw.from_native(pd.Series([1, 5, 3]), series_only=True)
>>> assert_series_equal(s1, s2)
Traceback (most recent call last):
...
AssertionError: Series are different (exact value mismatch)
[left]:
┌───────────────┐
|Narwhals Series|
|---------------|
| 0 1 |
| 1 2 |
| 2 3 |
| dtype: int64 |
└───────────────┘
[right]:
┌───────────────┐
|Narwhals Series|
|---------------|
| 0 1 |
| 1 5 |
| 2 3 |
| dtype: int64 |
└───────────────┘
Source code in narwhals/testing/asserts/series.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|