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
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
# 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)
  objects = lazy ? "LazyFrames" : "DataFrames"

  _assert_frame_schema_equal(
    left,
    right,
    check_column_order: check_column_order,
    check_dtype: check_dtype,
    objects: objects,
  )

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

  if left.height != right.height
    raise_assertion_error(
      objects, "number of rows does not match", left.height, right.height
    )
  end

  if !check_row_order
    left, right = _sort_dataframes(left, right)
  end

  left.columns.each do |c|
    s_left, s_right = left.get_column(c), right.get_column(c)
    begin
      _assert_series_values_equal(
        s_left,
        s_right,
        check_exact: check_exact,
        rtol: rtol,
        atol: atol,
        categorical_as_str: categorical_as_str
      )
    rescue AssertionError
      raise_assertion_error(
        objects,
        "value mismatch for column #{c.inspect}",
        s_left.to_a,
        s_right.to_a
      )
    end
  end
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)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/polars/testing.rb', line 116

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_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_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)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/polars/testing.rb', line 174

def assert_series_equal(
  left,
  right,
  check_dtype: true,
  check_names: 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

  if left.len != right.len
    raise_assertion_error("Series", "length mismatch", left.len, right.len)
  end

  if check_names && left.name != right.name
    raise_assertion_error("Series", "name mismatch", left.name, right.name)
  end

  if check_dtype && left.dtype != right.dtype
    raise_assertion_error("Series", "dtype mismatch", left.dtype, right.dtype)
  end

  _assert_series_values_equal(
    left,
    right,
    check_exact: check_exact,
    rtol: rtol,
    atol: atol,
    categorical_as_str: 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)


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/polars/testing.rb', line 241

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