-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fraction_same.py
More file actions
340 lines (309 loc) · 10.5 KB
/
test_fraction_same.py
File metadata and controls
340 lines (309 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
146
147
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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# Copyright (c) QuantCo 2025-2026
# SPDX-License-Identifier: BSD-3-Clause
import datetime as dt
import itertools
from collections import defaultdict
from collections.abc import Callable, Mapping
import polars as pl
import pytest
from polars.datatypes.group import (
FLOAT_DTYPES,
SIGNED_INTEGER_DTYPES,
UNSIGNED_INTEGER_DTYPES,
)
from diffly import PrimaryKeyError, compare_frames
from .utils import FRAME_TYPES, TYPING_FRAME_TYPES
def test_missing_primary_key_fraction_same() -> None:
left = pl.DataFrame({"id": ["a", "b", "c"], "value": [1, 2, 3]})
right = pl.DataFrame({"id": ["a", "b"], "value": [1, 2]})
comparison = compare_frames(left, right)
with pytest.raises(PrimaryKeyError):
_ = comparison.fraction_same("value")
@pytest.mark.parametrize("frame_type", FRAME_TYPES)
@pytest.mark.parametrize(
"dtypes_left",
itertools.zip_longest(
FLOAT_DTYPES,
SIGNED_INTEGER_DTYPES,
UNSIGNED_INTEGER_DTYPES,
fillvalue=pl.Float32,
),
)
@pytest.mark.parametrize(
"dtypes_right",
itertools.zip_longest(
FLOAT_DTYPES,
SIGNED_INTEGER_DTYPES,
UNSIGNED_INTEGER_DTYPES,
fillvalue=pl.Float32,
),
)
@pytest.mark.parametrize("parallel", [True, False])
def test_fraction_same(
frame_type: TYPING_FRAME_TYPES,
dtypes_left: tuple[pl.DataType, pl.DataType, pl.DataType],
dtypes_right: tuple[pl.DataType, pl.DataType, pl.DataType],
parallel: bool,
) -> None:
left_schema = {
"name": pl.String,
"author": pl.String,
"price": dtypes_left[0],
"n_copies": dtypes_left[2],
"n_change_in_stock": dtypes_left[1],
"publishing_date": pl.Date,
"metadata": pl.Struct({"genre": pl.String, "n_pages": dtypes_left[2]}),
"ratings": pl.List(dtypes_left[0]),
"is_fiction": pl.Boolean,
"v_left": dtypes_left[0],
}
right_schema = {
"name": pl.String,
"author": pl.String,
"price": dtypes_right[0],
"n_copies": dtypes_right[2],
"n_change_in_stock": dtypes_right[1],
"publishing_date": pl.Date,
"metadata": pl.Struct({"genre": pl.String, "n_pages": dtypes_right[2]}),
"ratings": pl.List(dtypes_right[0]),
"is_fiction": pl.Boolean,
"v_right": dtypes_right[0],
}
left = frame_type(
data={
"name": ["Book A", "Book B", "Book C", "Book D"],
"author": ["Author 1", "Author 2", "Author 3", "Author 4"],
"price": [10.0, 15.0, 20.0, 25.0],
"n_copies": [5, 3, 10, 2],
"n_change_in_stock": [1, -5, 0, 2],
"publishing_date": [
dt.date(2023, 1, 1),
dt.date(2023, 1, 2),
dt.date(2023, 1, 3),
dt.date(2023, 1, 4),
],
"metadata": [
{"genre": "Fiction", "n_pages": 30},
{"genre": "Non-Fiction", "n_pages": 20},
{"genre": "Fiction", "n_pages": 40},
{"genre": "Non-Fiction", "n_pages": 10},
],
"ratings": [
[4.5, 4.0, 5.0],
[3.0, 4.0, 2.5],
[5.0, 4.5],
[3.5, 4.0],
],
"is_fiction": [True, False, True, False],
"v_left": [1.0, 2.0, 3.0, 4.0],
},
schema=left_schema, # type: ignore
)
right = frame_type(
data={
"name": ["Book A", "Book B", "Book C", "Book E"],
"author": ["Author 1", "Author 2", "Author 3", "Author 4"],
"price": [10.0, 15.0, 19.99, 25.0],
"n_copies": [6, 29, 10, 2],
"n_change_in_stock": [0, 7, -9, 2],
"publishing_date": [
dt.date(2023, 1, 1),
dt.date(2025, 1, 2),
dt.date(2023, 1, 3),
dt.date(2023, 1, 4),
],
"metadata": [
{"genre": "Romance", "n_pages": 30},
{"genre": "Non-Fiction", "n_pages": 21},
{"genre": "Fiction", "n_pages": 40},
{"genre": "Non-Fiction", "n_pages": 15},
],
"ratings": [
[4.5, 4.0, 5.0],
[3.0, 4.2, 2.5],
[5.0, 4.5],
[3.5, 4.0],
],
"is_fiction": [True, False, True, False],
"v_right": [1.0, 2.0, 2.0, 2.0],
},
schema=right_schema, # type: ignore
)
comparison = compare_frames(left, right, primary_key=["name"])
expected = {
"author": 3 / 3,
"price": 2 / 3,
"n_copies": 1 / 3,
"n_change_in_stock": 0 / 3,
"publishing_date": 2 / 3,
"metadata": 1 / 3,
"ratings": 2 / 3,
"is_fiction": 3 / 3,
}
if parallel:
actual = comparison.fraction_same()
else:
actual = {col: comparison.fraction_same(col) for col in expected.keys()}
assert actual == expected
def test_fraction_same_join_column() -> None:
left = pl.DataFrame({"id": ["a", "b"], "value": [1, 2]})
right = pl.DataFrame({"id": ["a", "b"], "value": [1, 3]})
primary_key = "id"
comparison = compare_frames(left, right, primary_key=primary_key)
with pytest.raises(
ValueError,
match="is a join column for which a fraction of matching values cannot be computed.",
):
comparison.fraction_same(primary_key)
def test_fraction_same_no_non_join_columns() -> None:
df = pl.DataFrame({"id": ["a", "b", "c"]})
comparison = compare_frames(df, df, primary_key=["id"])
assert comparison.fraction_same() == dict()
def test_fraction_same_uncommon_column() -> None:
left = pl.DataFrame({"id": ["a", "b"], "value": [1, 2]})
right = pl.DataFrame({"id": ["a", "b"], "other": [3, 4]})
primary_key = ["id"]
comparison = compare_frames(left, right, primary_key=primary_key)
with pytest.raises(
ValueError,
match="is not a common column, so the fraction of matching values cannot be computed.",
):
comparison.fraction_same("nonexistent_column")
@pytest.mark.parametrize(
("perturbation", "abs_tol", "rel_tol"),
[
# NOTE: Double perturbations for tolerations to prevent odd floating point
# comparison issues
(lambda col: col + 0.01, 0.02, 1e-05),
(lambda col: col * 1.01, 1e-08, 0.02),
],
)
def test_float_tolerance_fraction_same(
perturbation: Callable[[pl.Expr], pl.Expr],
abs_tol: float,
rel_tol: float,
) -> None:
base = pl.DataFrame(
{"id": ["a", "b", "c"], "x": [1.0, None, 3.0], "y": [4.0, 5.0, 6.0]}
)
noisy = base.with_columns(x=None, y=perturbation(pl.col("y")))
primary_key = ["id"]
comparison_default = compare_frames(base, noisy, primary_key=primary_key)
assert comparison_default.fraction_same("x") == 1 / 3
assert comparison_default.fraction_same("y") == 0 / 3
comparison_lenient = compare_frames(
base,
noisy,
primary_key=primary_key,
abs_tol=abs_tol,
rel_tol=rel_tol,
)
assert comparison_lenient.fraction_same("y") == 1.0
@pytest.mark.parametrize(
"abs_tol",
[
defaultdict(lambda: 0.01, {"value2": 0.2}),
{"value": 0.01, "value2": 0.2},
],
ids=["defaultdict", "dict"],
)
def test_float_abs_tol_mapping_fraction_same(abs_tol: Mapping[str, float]) -> None:
df = pl.DataFrame(
data=[
("a", 2.0, 2.0),
("b", 3.0, 3.0),
("c", 4.0, 4.0),
],
schema={
"id": pl.String,
"value": pl.Float64,
"value2": pl.Float64,
},
orient="row",
)
df_pertubed = df.with_columns(pl.col("value") + 0.01, pl.col("value2") + 0.19)
comparison = compare_frames(df, df_pertubed, primary_key="id")
assert comparison.fraction_same("value") == 0.0
assert comparison.fraction_same("value2") == 0.0
comparison_lenient = compare_frames(
df,
df_pertubed,
primary_key="id",
abs_tol=abs_tol,
)
assert comparison_lenient.fraction_same("value") == 1.0
assert comparison_lenient.fraction_same("value2") == 1.0
@pytest.mark.parametrize(
"rtol",
[
defaultdict(lambda: 0.01, {"value2": 0.2}),
{"value": 0.01, "value2": 0.2},
],
ids=["defaultdict", "dict"],
)
def test_float_rtol_mapping_fraction_same(rtol: Mapping[str, float]) -> None:
df = pl.DataFrame(
data=[
("a", 2.0, 2.0),
("b", 3.0, 3.0),
("c", 4.0, 4.0),
],
schema={
"id": pl.String,
"value": pl.Float64,
"value2": pl.Float64,
},
orient="row",
)
df_pertubed = df.with_columns(pl.col("value") * 1.01, pl.col("value2") * 1.19)
comparison = compare_frames(df, df_pertubed, primary_key="id")
assert comparison.fraction_same("value") == 0.0
assert comparison.fraction_same("value2") == 0.0
comparison_lenient = compare_frames(
df,
df_pertubed,
primary_key="id",
rel_tol=rtol,
)
assert comparison_lenient.fraction_same("value") == 1.0
assert comparison_lenient.fraction_same("value2") == 1.0
@pytest.mark.parametrize(
"abs_tol_temporal",
[
defaultdict(
lambda: dt.timedelta(seconds=1), {"value2": dt.timedelta(seconds=2)}
),
{"value": dt.timedelta(seconds=1), "value2": dt.timedelta(seconds=2)},
],
ids=["defaultdict", "dict"],
)
def test_abs_tol_temporal_mapping_fraction_same(
abs_tol_temporal: Mapping[str, dt.timedelta],
) -> None:
df = pl.DataFrame(
data=[
("a", dt.datetime(2025, 1, 1, 0, 0, 0), dt.datetime(2025, 1, 1, 0, 0, 0)),
("b", dt.datetime(2025, 1, 2, 0, 0, 0), dt.datetime(2025, 1, 2, 0, 0, 0)),
("c", dt.datetime(2025, 1, 3, 0, 0, 0), dt.datetime(2025, 1, 3, 0, 0, 0)),
],
schema={
"id": pl.String,
"value": pl.Datetime,
"value2": pl.Datetime,
},
orient="row",
)
df_pertubed = df.with_columns(
pl.col("value").dt.offset_by("1s"), pl.col("value2").dt.offset_by("2s")
)
comparison = compare_frames(df, df_pertubed, primary_key="id")
assert comparison.fraction_same("value") == 0.0
assert comparison.fraction_same("value2") == 0.0
comparison_lenient = compare_frames(
df,
df_pertubed,
primary_key="id",
abs_tol_temporal=abs_tol_temporal,
)
assert comparison_lenient.fraction_same("value") == 1.0
assert comparison_lenient.fraction_same("value2") == 1.0