Module: Polars::Testing

Defined in:
lib/polars/testing.rb

Instance Method Summary collapse

Instance Method Details

#assert_frame_equal(left, right, check_row_order: true, check_column_order: true, check_dtype: true, check_exact: false, rtol: 1e-5, atol: 1e-8, categorical_as_str: false) ⇒ nil

Assert that the left and right frame are equal.

Raises a detailed AssertionError if the frames differ. This function is intended for use in unit tests.

Parameters:

  • left (Object)

    The first DataFrame or LazyFrame to compare.

  • right (Object)

    The second DataFrame or LazyFrame to compare.

  • check_row_order (Boolean) (defaults to: true)

    Require row order to match.

  • check_column_order (Boolean) (defaults to: true)

    Require column order to match.

  • check_dtype (Boolean) (defaults to: true)

    Require data types to match.

  • check_exact (Boolean) (defaults to: false)

    Require float values to match exactly. If set to false, values are considered equal when within tolerance of each other (see rtol and atol). Only affects columns with a Float data type.

  • rtol (Float) (defaults to: 1e-5)

    Relative tolerance for inexact checking. Fraction of values in right.

  • atol (Float) (defaults to: 1e-8)

    Absolute tolerance for inexact checking.

  • categorical_as_str (Boolean) (defaults to: false)

    Cast categorical columns to string before comparing. Enabling this helps compare columns that do not share the same string cache.

Returns:

  • (nil)


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
# File 'lib/polars/testing.rb', line 31

def assert_frame_equal(
  left,
  right,
  check_row_order: true,
  check_column_order: true,
  check_dtype: true,
  check_exact: false,
  rtol: 1e-5,
  atol: 1e-8,
  categorical_as_str: false
)
  lazy = _assert_correct_input_type(left, right)

  if lazy
    left, right = left.collect, right.collect
  end

  Plr.assert_dataframe_equal_rb(
    left._df,
    right._df,
    check_row_order,
    check_column_order,
    check_dtype,
    check_exact,
    rtol,
    atol,
    categorical_as_str,
  )
end

#assert_frame_not_equal(left, right, check_row_order: true, check_column_order: true, check_dtype: true, check_exact: false, rtol: 1e-5, atol: 1e-8, categorical_as_str: false) ⇒ nil

Assert that the left and right frame are not equal.

This function is intended for use in unit tests.

Parameters:

  • left (Object)

    The first DataFrame or LazyFrame to compare.

  • right (Object)

    The second DataFrame or LazyFrame to compare.

  • check_row_order (Boolean) (defaults to: true)

    Require row order to match.

  • check_column_order (Boolean) (defaults to: true)

    Require column order to match.

  • check_dtype (Boolean) (defaults to: true)

    Require data types to match.

  • check_exact (Boolean) (defaults to: false)

    Require float values to match exactly. If set to false, values are considered equal when within tolerance of each other (see rtol and atol). Only affects columns with a Float data type.

  • rtol (Float) (defaults to: 1e-5)

    Relative tolerance for inexact checking. Fraction of values in right.

  • atol (Float) (defaults to: 1e-8)

    Absolute tolerance for inexact checking.

  • categorical_as_str (Boolean) (defaults to: false)

    Cast categorical columns to string before comparing. Enabling this helps compare columns that do not share the same string cache.

Returns:

  • (nil)

Raises:

  • (AssertionError)


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
116
117
# File 'lib/polars/testing.rb', line 88

def assert_frame_not_equal(
  left,
  right,
  check_row_order: true,
  check_column_order: true,
  check_dtype: true,
  check_exact: false,
  rtol: 1e-5,
  atol: 1e-8,
  categorical_as_str: false
)
  begin
    assert_frame_equal(
      left,
      right,
      check_column_order: check_column_order,
      check_row_order: check_row_order,
      check_dtype: check_dtype,
      check_exact: check_exact,
      rtol: rtol,
      atol: atol,
      categorical_as_str: categorical_as_str
    )
  rescue AssertionError
    return
  end

  msg = "frames are equal"
  raise AssertionError, msg
end

#assert_series_equal(left, right, check_dtype: true, check_names: true, check_order: true, check_exact: false, rtol: 1e-5, atol: 1e-8, categorical_as_str: false) ⇒ nil

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:

  • left (Object)

    The first Series to compare.

  • right (Object)

    The second Series to compare.

  • check_dtype (Boolean) (defaults to: true)

    Require data types to match.

  • check_names (Boolean) (defaults to: true)

    Require names to match.

  • check_order (Boolean) (defaults to: true)

    Requires elements to appear in the same order.

  • check_exact (Boolean) (defaults to: false)

    Require float values to match exactly. If set to false, values are considered equal when within tolerance of each other (see rtol and atol). Only affects columns with a Float data type.

  • rtol (Float) (defaults to: 1e-5)

    Relative tolerance for inexact checking, given as a fraction of the values in right.

  • atol (Float) (defaults to: 1e-8)

    Absolute tolerance for inexact checking.

  • categorical_as_str (Boolean) (defaults to: false)

    Cast categorical columns to string before comparing. Enabling this helps compare columns that do not share the same string cache.

Returns:

  • (nil)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/polars/testing.rb', line 148

def assert_series_equal(
  left,
  right,
  check_dtype: true,
  check_names: true,
  check_order: true,
  check_exact: false,
  rtol: 1e-5,
  atol: 1e-8,
  categorical_as_str: false
)
  if !(left.is_a?(Series) && right.is_a?(Series))
    raise_assertion_error(
      "inputs",
      "unexpected input types",
      left.class.name,
      right.class.name
    )
  end

  Plr.assert_series_equal_rb(
    left._s,
    right._s,
    check_dtype,
    check_names,
    check_order,
    check_exact,
    rtol,
    atol,
    categorical_as_str
  )
end

#assert_series_not_equal(left, right, check_dtype: true, check_names: true, check_exact: false, rtol: 1e-5, atol: 1e-8, categorical_as_str: false) ⇒ nil

Assert that the left and right Series are not equal.

This function is intended for use in unit tests.

Parameters:

  • left (Object)

    The first Series to compare.

  • right (Object)

    The second Series to compare.

  • check_dtype (Boolean) (defaults to: true)

    Require data types to match.

  • check_names (Boolean) (defaults to: true)

    Require names to match.

  • check_exact (Boolean) (defaults to: false)

    Require float values to match exactly. If set to false, values are considered equal when within tolerance of each other (see rtol and atol). Only affects columns with a Float data type.

  • rtol (Float) (defaults to: 1e-5)

    Relative tolerance for inexact checking, given as a fraction of the values in right.

  • atol (Float) (defaults to: 1e-8)

    Absolute tolerance for inexact checking.

  • categorical_as_str (Boolean) (defaults to: false)

    Cast categorical columns to string before comparing. Enabling this helps compare columns that do not share the same string cache.

Returns:

  • (nil)

Raises:

  • (AssertionError)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/polars/testing.rb', line 207

def assert_series_not_equal(
  left,
  right,
  check_dtype: true,
  check_names: true,
  check_exact: false,
  rtol: 1e-5,
  atol: 1e-8,
  categorical_as_str: false
)
  begin
    assert_series_equal(
      left,
      right,
      check_dtype: check_dtype,
      check_names: check_names,
      check_exact: check_exact,
      rtol: rtol,
      atol: atol,
      categorical_as_str: categorical_as_str
    )
  rescue AssertionError
    return
  end

  msg = "Series are equal"
  raise AssertionError, msg
end