-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_join.py
837 lines (693 loc) · 25.3 KB
/
test_join.py
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
from __future__ import annotations
from datetime import date, datetime
from typing import TYPE_CHECKING, Literal
import numpy as np
import pandas as pd
import pytest
import polars as pl
from polars.testing import assert_frame_equal, assert_series_equal
if TYPE_CHECKING:
from polars.type_aliases import JoinStrategy
def test_semi_anti_join() -> None:
df_a = pl.DataFrame({"key": [1, 2, 3], "payload": ["f", "i", None]})
df_b = pl.DataFrame({"key": [3, 4, 5, None]})
assert df_a.join(df_b, on="key", how="anti").to_dict(as_series=False) == {
"key": [1, 2],
"payload": ["f", "i"],
}
assert df_a.join(df_b, on="key", how="semi").to_dict(as_series=False) == {
"key": [3],
"payload": [None],
}
# lazy
result = df_a.lazy().join(df_b.lazy(), on="key", how="anti").collect()
expected_values = {"key": [1, 2], "payload": ["f", "i"]}
assert result.to_dict(as_series=False) == expected_values
result = df_a.lazy().join(df_b.lazy(), on="key", how="semi").collect()
expected_values = {"key": [3], "payload": [None]}
assert result.to_dict(as_series=False) == expected_values
df_a = pl.DataFrame(
{"a": [1, 2, 3, 1], "b": ["a", "b", "c", "a"], "payload": [10, 20, 30, 40]}
)
df_b = pl.DataFrame({"a": [3, 3, 4, 5], "b": ["c", "c", "d", "e"]})
assert df_a.join(df_b, on=["a", "b"], how="anti").to_dict(as_series=False) == {
"a": [1, 2, 1],
"b": ["a", "b", "a"],
"payload": [10, 20, 40],
}
assert df_a.join(df_b, on=["a", "b"], how="semi").to_dict(as_series=False) == {
"a": [3],
"b": ["c"],
"payload": [30],
}
def test_join_same_cat_src() -> None:
df = pl.DataFrame(
data={"column": ["a", "a", "b"], "more": [1, 2, 3]},
schema=[("column", pl.Categorical), ("more", pl.Int32)],
)
df_agg = df.group_by("column").agg(pl.col("more").mean())
assert df.join(df_agg, on="column").to_dict(as_series=False) == {
"column": ["a", "a", "b"],
"more": [1, 2, 3],
"more_right": [1.5, 1.5, 3.0],
}
@pytest.mark.parametrize("reverse", [False, True])
def test_sorted_merge_joins(reverse: bool) -> None:
n = 30
df_a = pl.DataFrame({"a": np.sort(np.random.randint(0, n // 2, n))}).with_row_index(
"row_a"
)
df_b = pl.DataFrame(
{"a": np.sort(np.random.randint(0, n // 2, n // 2))}
).with_row_index("row_b")
if reverse:
df_a = df_a.select(pl.all().reverse())
df_b = df_b.select(pl.all().reverse())
join_strategies: list[JoinStrategy] = ["left", "inner"]
for cast_to in [int, str, float]:
for how in join_strategies:
df_a_ = df_a.with_columns(pl.col("a").cast(cast_to))
df_b_ = df_b.with_columns(pl.col("a").cast(cast_to))
# hash join
out_hash_join = df_a_.join(df_b_, on="a", how=how)
# sorted merge join
out_sorted_merge_join = df_a_.with_columns(
pl.col("a").set_sorted(descending=reverse)
).join(
df_b_.with_columns(pl.col("a").set_sorted(descending=reverse)),
on="a",
how=how,
)
assert_frame_equal(out_hash_join, out_sorted_merge_join)
def test_join_negative_integers() -> None:
expected = {"a": [-6, -1, 0], "b": [-6, -1, 0]}
df1 = pl.DataFrame(
{
"a": [-1, -6, -3, 0],
}
)
df2 = pl.DataFrame(
{
"a": [-6, -1, -4, -2, 0],
"b": [-6, -1, -4, -2, 0],
}
)
for dt in [pl.Int8, pl.Int16, pl.Int32, pl.Int64]:
assert (
df1.with_columns(pl.all().cast(dt))
.join(df2.with_columns(pl.all().cast(dt)), on="a", how="inner")
.to_dict(as_series=False)
== expected
)
def test_deprecated() -> None:
df = pl.DataFrame({"a": [1, 2], "b": [3, 4]})
other = pl.DataFrame({"a": [1, 2], "c": [3, 4]})
result = pl.DataFrame({"a": [1, 2], "b": [3, 4], "c": [3, 4]})
np.testing.assert_equal(df.join(other=other, on="a").to_numpy(), result.to_numpy())
np.testing.assert_equal(
df.lazy().join(other=other.lazy(), on="a").collect().to_numpy(),
result.to_numpy(),
)
def test_join_on_expressions() -> None:
df_a = pl.DataFrame({"a": [1, 2, 3]})
df_b = pl.DataFrame({"b": [1, 4, 9, 9, 0]})
assert df_a.join(df_b, left_on=(pl.col("a") ** 2).cast(int), right_on=pl.col("b"))[
"a"
].to_list() == [1, 4, 9, 9]
def test_join() -> None:
df_left = pl.DataFrame(
{
"a": ["a", "b", "a", "z"],
"b": [1, 2, 3, 4],
"c": [6, 5, 4, 3],
}
)
df_right = pl.DataFrame(
{
"a": ["b", "c", "b", "a"],
"k": [0, 3, 9, 6],
"c": [1, 0, 2, 1],
}
)
joined = df_left.join(df_right, left_on="a", right_on="a").sort("a")
assert_series_equal(joined["b"], pl.Series("b", [1, 3, 2, 2]))
joined = df_left.join(df_right, left_on="a", right_on="a", how="left").sort("a")
assert joined["c_right"].is_null().sum() == 1
assert_series_equal(joined["b"], pl.Series("b", [1, 3, 2, 2, 4]))
joined = df_left.join(df_right, left_on="a", right_on="a", how="outer").sort("a")
assert joined["c_right"].null_count() == 1
assert joined["c"].null_count() == 1
assert joined["b"].null_count() == 1
assert joined["k"].null_count() == 1
assert joined["a"].null_count() == 1
# we need to pass in a column to join on, either by supplying `on`, or both
# `left_on` and `right_on`
with pytest.raises(ValueError):
df_left.join(df_right)
with pytest.raises(ValueError):
df_left.join(df_right, right_on="a")
with pytest.raises(ValueError):
df_left.join(df_right, left_on="a")
df_a = pl.DataFrame({"a": [1, 2, 1, 1], "b": ["a", "b", "c", "c"]})
df_b = pl.DataFrame(
{"foo": [1, 1, 1], "bar": ["a", "c", "c"], "ham": ["let", "var", "const"]}
)
# just check if join on multiple columns runs
df_a.join(df_b, left_on=["a", "b"], right_on=["foo", "bar"])
eager_join = df_a.join(df_b, left_on="a", right_on="foo")
lazy_join = df_a.lazy().join(df_b.lazy(), left_on="a", right_on="foo").collect()
cols = ["a", "b", "bar", "ham"]
assert lazy_join.shape == eager_join.shape
assert_frame_equal(lazy_join.sort(by=cols), eager_join.sort(by=cols))
def test_joins_dispatch() -> None:
# this just flexes the dispatch a bit
# don't change the data of this dataframe, this triggered:
# https://github.com/pola-rs/polars/issues/1688
dfa = pl.DataFrame(
{
"a": ["a", "b", "c", "a"],
"b": [1, 2, 3, 1],
"date": ["2021-01-01", "2021-01-02", "2021-01-03", "2021-01-01"],
"datetime": [13241324, 12341256, 12341234, 13241324],
}
).with_columns(
[pl.col("date").str.strptime(pl.Date), pl.col("datetime").cast(pl.Datetime)]
)
join_strategies: list[JoinStrategy] = ["left", "inner", "outer"]
for how in join_strategies:
dfa.join(dfa, on=["a", "b", "date", "datetime"], how=how)
dfa.join(dfa, on=["date", "datetime"], how=how)
dfa.join(dfa, on=["date", "datetime", "a"], how=how)
dfa.join(dfa, on=["date", "a"], how=how)
dfa.join(dfa, on=["a", "datetime"], how=how)
dfa.join(dfa, on=["date"], how=how)
def test_join_on_cast() -> None:
df_a = (
pl.DataFrame({"a": [-5, -2, 3, 3, 9, 10]})
.with_row_index()
.with_columns(pl.col("a").cast(pl.Int32))
)
df_b = pl.DataFrame({"a": [-2, -3, 3, 10]})
assert df_a.join(df_b, on=pl.col("a").cast(pl.Int64)).to_dict(as_series=False) == {
"index": [1, 2, 3, 5],
"a": [-2, 3, 3, 10],
}
assert df_a.lazy().join(
df_b.lazy(), on=pl.col("a").cast(pl.Int64)
).collect().to_dict(as_series=False) == {
"index": [1, 2, 3, 5],
"a": [-2, 3, 3, 10],
}
def test_join_chunks_alignment_4720() -> None:
df1 = pl.DataFrame(
{
"index1": pl.arange(0, 2, eager=True),
"index2": pl.arange(10, 12, eager=True),
}
)
df2 = pl.DataFrame(
{
"index3": pl.arange(100, 102, eager=True),
}
)
df3 = pl.DataFrame(
{
"index1": pl.arange(0, 2, eager=True),
"index2": pl.arange(10, 12, eager=True),
"index3": pl.arange(100, 102, eager=True),
}
)
assert (
df1.join(df2, how="cross").join(
df3,
on=["index1", "index2", "index3"],
how="left",
)
).to_dict(as_series=False) == {
"index1": [0, 0, 1, 1],
"index2": [10, 10, 11, 11],
"index3": [100, 101, 100, 101],
}
assert (
df1.join(df2, how="cross").join(
df3,
on=["index3", "index1", "index2"],
how="left",
)
).to_dict(as_series=False) == {
"index1": [0, 0, 1, 1],
"index2": [10, 10, 11, 11],
"index3": [100, 101, 100, 101],
}
def test_sorted_flag_after_joins() -> None:
np.random.seed(1)
dfa = pl.DataFrame(
{
"a": np.random.randint(0, 13, 20),
"b": np.random.randint(0, 13, 20),
}
).sort("a")
dfb = pl.DataFrame(
{
"a": np.random.randint(0, 13, 10),
"b": np.random.randint(0, 13, 10),
}
)
dfapd = dfa.to_pandas()
dfbpd = dfb.to_pandas()
def test_with_pd(
dfa: pd.DataFrame, dfb: pd.DataFrame, on: str, how: str, joined: pl.DataFrame
) -> None:
a = (
dfa.merge(
dfb,
on=on,
how=how, # type: ignore[arg-type]
suffixes=("", "_right"),
)
.sort_values(["a", "b"])
.reset_index(drop=True)
)
b = joined.sort(["a", "b"]).to_pandas()
pd.testing.assert_frame_equal(a, b)
joined = dfa.join(dfb, on="b", how="left")
assert joined["a"].flags["SORTED_ASC"]
test_with_pd(dfapd, dfbpd, "b", "left", joined)
joined = dfa.join(dfb, on="b", how="inner")
assert joined["a"].flags["SORTED_ASC"]
test_with_pd(dfapd, dfbpd, "b", "inner", joined)
joined = dfa.join(dfb, on="b", how="semi")
assert joined["a"].flags["SORTED_ASC"]
joined = dfa.join(dfb, on="b", how="semi")
assert joined["a"].flags["SORTED_ASC"]
joined = dfb.join(dfa, on="b", how="left")
assert not joined["a"].flags["SORTED_ASC"]
test_with_pd(dfbpd, dfapd, "b", "left", joined)
joined = dfb.join(dfa, on="b", how="inner")
assert not joined["a"].flags["SORTED_ASC"]
joined = dfb.join(dfa, on="b", how="semi")
assert not joined["a"].flags["SORTED_ASC"]
joined = dfb.join(dfa, on="b", how="anti")
assert not joined["a"].flags["SORTED_ASC"]
def test_jit_sort_joins() -> None:
n = 200
# Explicitly specify numpy dtype because of different defaults on Windows
dfa = pd.DataFrame(
{
"a": np.random.randint(0, 100, n, dtype=np.int64),
"b": np.arange(0, n, dtype=np.int64),
}
)
n = 40
dfb = pd.DataFrame(
{
"a": np.random.randint(0, 100, n, dtype=np.int64),
"b": np.arange(0, n, dtype=np.int64),
}
)
dfa_pl = pl.from_pandas(dfa).sort("a")
dfb_pl = pl.from_pandas(dfb)
join_strategies: list[Literal["left", "inner"]] = ["left", "inner"]
for how in join_strategies:
pd_result = dfa.merge(dfb, on="a", how=how)
pd_result.columns = pd.Index(["a", "b", "b_right"])
# left key sorted right is not
pl_result = dfa_pl.join(dfb_pl, on="a", how=how).sort(["a", "b"])
a = pl.from_pandas(pd_result).with_columns(pl.all().cast(int)).sort(["a", "b"])
assert_frame_equal(a, pl_result)
assert pl_result["a"].flags["SORTED_ASC"]
# left key sorted right is not
pd_result = dfb.merge(dfa, on="a", how=how)
pd_result.columns = pd.Index(["a", "b", "b_right"])
pl_result = dfb_pl.join(dfa_pl, on="a", how=how).sort(["a", "b"])
a = pl.from_pandas(pd_result).with_columns(pl.all().cast(int)).sort(["a", "b"])
assert_frame_equal(a, pl_result)
assert pl_result["a"].flags["SORTED_ASC"]
def test_join_panic_on_binary_expr_5915() -> None:
df_a = pl.DataFrame({"a": [1, 2, 3]}).lazy()
df_b = pl.DataFrame({"b": [1, 4, 9, 9, 0]}).lazy()
z = df_a.join(df_b, left_on=[(pl.col("a") + 1).cast(int)], right_on=[pl.col("b")])
assert z.collect().to_dict(as_series=False) == {"a": [4]}
def test_semi_join_projection_pushdown_6423() -> None:
df1 = pl.DataFrame({"x": [1]}).lazy()
df2 = pl.DataFrame({"y": [1], "x": [1]}).lazy()
assert (
df1.join(df2, left_on="x", right_on="y", how="semi")
.join(df2, left_on="x", right_on="y", how="semi")
.select(["x"])
).collect().to_dict(as_series=False) == {"x": [1]}
def test_semi_join_projection_pushdown_6455() -> None:
df = pl.DataFrame(
{
"id": [1, 1, 2],
"timestamp": [
datetime(2022, 12, 11),
datetime(2022, 12, 12),
datetime(2022, 1, 1),
],
"value": [1, 2, 4],
}
).lazy()
latest = df.group_by("id").agg(pl.col("timestamp").max())
df = df.join(latest, on=["id", "timestamp"], how="semi")
assert df.select(["id", "value"]).collect().to_dict(as_series=False) == {
"id": [1, 2],
"value": [2, 4],
}
def test_update() -> None:
df1 = pl.DataFrame(
{
"key1": [1, 2, 3, 4],
"key2": [1, 2, 3, 4],
"a": [1, 2, 3, 4],
"b": [1, 2, 3, 4],
"c": ["1", "2", "3", "4"],
"d": [
date(2023, 1, 1),
date(2023, 1, 2),
date(2023, 1, 3),
date(2023, 1, 4),
],
}
)
df2 = pl.DataFrame(
{
"key1": [1, 2, 3, 4],
"key2": [1, 2, 3, 5],
"a": [1, 1, 1, 1],
"b": [2, 2, 2, 2],
"c": ["3", "3", "3", "3"],
"d": [
date(2023, 5, 5),
date(2023, 5, 5),
date(2023, 5, 5),
date(2023, 5, 5),
],
}
)
# update only on key1
expected = pl.DataFrame(
{
"key1": [1, 2, 3, 4],
"key2": [1, 2, 3, 5],
"a": [1, 1, 1, 1],
"b": [2, 2, 2, 2],
"c": ["3", "3", "3", "3"],
"d": [
date(2023, 5, 5),
date(2023, 5, 5),
date(2023, 5, 5),
date(2023, 5, 5),
],
}
)
assert_frame_equal(df1.update(df2, on="key1"), expected)
# update on key1 using different left/right names
assert_frame_equal(
df1.update(
df2.rename({"key1": "key1b"}),
left_on="key1",
right_on="key1b",
),
expected,
)
# update on key1 and key2. This should fail to match the last item.
expected = pl.DataFrame(
{
"key1": [1, 2, 3, 4],
"key2": [1, 2, 3, 4],
"a": [1, 1, 1, 4],
"b": [2, 2, 2, 4],
"c": ["3", "3", "3", "4"],
"d": [
date(2023, 5, 5),
date(2023, 5, 5),
date(2023, 5, 5),
date(2023, 1, 4),
],
}
)
assert_frame_equal(df1.update(df2, on=["key1", "key2"]), expected)
# update on key1 and key2 using different left/right names
assert_frame_equal(
df1.update(
df2.rename({"key1": "key1b", "key2": "key2b"}),
left_on=["key1", "key2"],
right_on=["key1b", "key2b"],
),
expected,
)
df = pl.DataFrame({"A": [1, 2, 3, 4], "B": [400, 500, 600, 700]})
new_df = pl.DataFrame({"B": [4, None, 6], "C": [7, 8, 9]})
assert df.update(new_df).to_dict(as_series=False) == {
"A": [1, 2, 3, 4],
"B": [4, 500, 6, 700],
}
df1 = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
df2 = pl.DataFrame({"a": [2, 3], "b": [8, 9]})
assert df1.update(df2, on="a").to_dict(as_series=False) == {
"a": [1, 2, 3],
"b": [4, 8, 9],
}
a = pl.LazyFrame({"a": [1, 2, 3]})
b = pl.LazyFrame({"b": [4, 5], "c": [3, 1]})
c = a.update(b)
assert_frame_equal(a, c)
# check behaviour of 'how' param
assert [1, 2, 3] == list(
a.update(b, left_on="a", right_on="c").collect().to_series()
)
assert [1, 3] == list(
a.update(b, how="inner", left_on="a", right_on="c").collect().to_series()
)
print(a, b)
print(a.update(b.rename({"b": "a"}), how="outer", on="a").collect())
assert [1, 2, 3, 4, 5] == sorted(
a.update(b.rename({"b": "a"}), how="outer", on="a").collect().to_series()
)
# check behavior of include_nulls=True
df = pl.DataFrame(
{
"A": [1, 2, 3, 4],
"B": [400, 500, 600, 700],
}
)
new_df = pl.DataFrame(
{
"B": [-66, None, -99],
"C": [5, 3, 1],
}
)
out = df.update(new_df, left_on="A", right_on="C", how="outer", include_nulls=True)
expected = pl.DataFrame(
{
"A": [1, 2, 3, 4, 5],
"B": [-99, 500, None, 700, -66],
}
)
assert_frame_equal(out, expected)
# edge-case #11684
x = pl.DataFrame({"a": [0, 1]})
y = pl.DataFrame({"a": [2, 3]})
assert [0, 1, 2, 3] == sorted(x.update(y, on="a", how="outer")["a"].to_list())
# disallowed join strategies
for join_strategy in ("cross", "anti", "semi"):
with pytest.raises(
ValueError,
match=f"`how` must be one of {{'left', 'inner', 'outer'}}; found '{join_strategy}'",
):
a.update(b, how=join_strategy) # type: ignore[arg-type]
def test_join_frame_consistency() -> None:
df = pl.DataFrame({"A": [1, 2, 3]})
ldf = pl.DataFrame({"A": [1, 2, 5]}).lazy()
with pytest.raises(TypeError, match="expected `other`.* LazyFrame"):
_ = ldf.join(df, on="A") # type: ignore[arg-type]
with pytest.raises(TypeError, match="expected `other`.* DataFrame"):
_ = df.join(ldf, on="A") # type: ignore[arg-type]
with pytest.raises(TypeError, match="expected `other`.* LazyFrame"):
_ = ldf.join_asof(df, on="A") # type: ignore[arg-type]
with pytest.raises(TypeError, match="expected `other`.* DataFrame"):
_ = df.join_asof(ldf, on="A") # type: ignore[arg-type]
def test_join_concat_projection_pd_case_7071() -> None:
ldf = pl.DataFrame({"id": [1, 2], "value": [100, 200]}).lazy()
ldf2 = pl.DataFrame({"id": [1, 3], "value": [100, 300]}).lazy()
ldf = ldf.join(ldf2, on=["id", "value"])
ldf = pl.concat([ldf, ldf2])
result = ldf.select("id")
expected = pl.DataFrame({"id": [1, 1, 3]}).lazy()
assert_frame_equal(result, expected)
def test_join_sorted_fast_paths_null() -> None:
df1 = pl.DataFrame({"x": [0, 1, 0]}).sort("x")
df2 = pl.DataFrame({"x": [0, None], "y": [0, 1]})
assert df1.join(df2, on="x", how="inner").to_dict(as_series=False) == {
"x": [0, 0],
"y": [0, 0],
}
assert df1.join(df2, on="x", how="left").to_dict(as_series=False) == {
"x": [0, 0, 1],
"y": [0, 0, None],
}
assert df1.join(df2, on="x", how="anti").to_dict(as_series=False) == {"x": [1]}
assert df1.join(df2, on="x", how="semi").to_dict(as_series=False) == {"x": [0, 0]}
assert df1.join(df2, on="x", how="outer").to_dict(as_series=False) == {
"x": [0, 0, 1, None],
"x_right": [0, 0, None, None],
"y": [0, 0, None, 1],
}
def test_outer_join_list_() -> None:
schema = {"id": pl.Int64, "vals": pl.List(pl.Float64)}
df1 = pl.DataFrame({"id": [1], "vals": [[]]}, schema=schema) # type: ignore[arg-type]
df2 = pl.DataFrame({"id": [2, 3], "vals": [[], [4]]}, schema=schema) # type: ignore[arg-type]
assert df1.join(df2, on="id", how="outer").to_dict(as_series=False) == {
"id": [None, None, 1],
"vals": [None, None, []],
"id_right": [2, 3, None],
"vals_right": [[], [4.0], None],
}
@pytest.mark.slow()
def test_join_validation() -> None:
def test_each_join_validation(
unique: pl.DataFrame, duplicate: pl.DataFrame, on: str, how: JoinStrategy
) -> None:
# one_to_many
_one_to_many_success_inner = unique.join(
duplicate, on=on, how=how, validate="1:m"
)
with pytest.raises(pl.ComputeError):
_one_to_many_fail_inner = duplicate.join(
unique, on=on, how=how, validate="1:m"
)
# one to one
with pytest.raises(pl.ComputeError):
_one_to_one_fail_1_inner = unique.join(
duplicate, on=on, how=how, validate="1:1"
)
with pytest.raises(pl.ComputeError):
_one_to_one_fail_2_inner = duplicate.join(
unique, on=on, how=how, validate="1:1"
)
# many to one
with pytest.raises(pl.ComputeError):
_many_to_one_fail_inner = unique.join(
duplicate, on=on, how=how, validate="m:1"
)
_many_to_one_success_inner = duplicate.join(
unique, on=on, how=how, validate="m:1"
)
# many to many
_many_to_many_success_1_inner = duplicate.join(
unique, on=on, how=how, validate="m:m"
)
_many_to_many_success_2_inner = unique.join(
duplicate, on=on, how=how, validate="m:m"
)
# test data
short_unique = pl.DataFrame(
{
"id": [1, 2, 3, 4],
"id_str": ["1", "2", "3", "4"],
"name": ["hello", "world", "rust", "polars"],
}
)
short_duplicate = pl.DataFrame(
{"id": [1, 2, 3, 1], "id_str": ["1", "2", "3", "1"], "cnt": [2, 4, 6, 1]}
)
long_unique = pl.DataFrame(
{
"id": [1, 2, 3, 4, 5],
"id_str": ["1", "2", "3", "4", "5"],
"name": ["hello", "world", "rust", "polars", "meow"],
}
)
long_duplicate = pl.DataFrame(
{
"id": [1, 2, 3, 1, 5],
"id_str": ["1", "2", "3", "1", "5"],
"cnt": [2, 4, 6, 1, 8],
}
)
join_strategies: list[JoinStrategy] = ["inner", "outer", "left"]
for join_col in ["id", "id_str"]:
for how in join_strategies:
# same size
test_each_join_validation(long_unique, long_duplicate, join_col, how)
# left longer
test_each_join_validation(long_unique, short_duplicate, join_col, how)
# right longer
test_each_join_validation(short_unique, long_duplicate, join_col, how)
def test_outer_join_bool() -> None:
df1 = pl.DataFrame({"id": [True, False], "val": [1, 2]})
df2 = pl.DataFrame({"id": [True, False], "val": [0, -1]})
assert df1.join(df2, on="id", how="outer").to_dict(as_series=False) == {
"id": [True, False],
"val": [1, 2],
"id_right": [True, False],
"val_right": [0, -1],
}
def test_outer_join_coalesce_different_names_13450() -> None:
df1 = pl.DataFrame({"L1": ["a", "b", "c"], "L3": ["b", "c", "d"], "L2": [1, 2, 3]})
df2 = pl.DataFrame({"L3": ["a", "c", "d"], "R2": [7, 8, 9]})
expected = pl.DataFrame(
{
"L1": ["a", "c", "d", "b"],
"L3": ["b", "d", None, "c"],
"L2": [1, 3, None, 2],
"R2": [7, 8, 9, None],
}
)
out = df1.join(df2, left_on="L1", right_on="L3", how="outer_coalesce")
assert_frame_equal(out, expected)
# https://github.com/pola-rs/polars/issues/10663
def test_join_on_wildcard_error() -> None:
df = pl.DataFrame({"x": [1]})
df2 = pl.DataFrame({"x": [1], "y": [2]})
with pytest.raises(
pl.ComputeError, match="wildcard column selection not supported at this point"
):
df.join(df2, on=pl.all())
def test_join_on_nth_error() -> None:
df = pl.DataFrame({"x": [1]})
df2 = pl.DataFrame({"x": [1], "y": [2]})
with pytest.raises(
pl.ComputeError, match="nth column selection not supported at this point"
):
df.join(df2, on=pl.first())
def test_join_results_in_duplicate_names() -> None:
lhs = pl.DataFrame(
{
"a": [1, 2, 3],
"b": [4, 5, 6],
"c": [1, 2, 3],
"c_right": [1, 2, 3],
}
)
rhs = lhs.clone()
with pytest.raises(pl.DuplicateError, match="'c_right' already exists"):
lhs.join(rhs, on=["a", "b"], how="left")
def test_join_projection_invalid_name_contains_suffix_15243() -> None:
df1 = pl.DataFrame({"a": [1, 2, 3]}).lazy()
df2 = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}).lazy()
with pytest.raises(pl.ColumnNotFoundError):
(
df1.join(df2, on="a")
.select(pl.col("b").filter(pl.col("b") == pl.col("foo_right")))
.collect()
)
def test_join_list_non_numeric() -> None:
assert (
pl.DataFrame(
{
"lists": [
["a", "b", "c"],
["a", "c", "b"],
["a", "c", "b"],
["a", "c", "d"],
]
}
)
).group_by("lists", maintain_order=True).agg(pl.len().alias("count")).to_dict(
as_series=False
) == {
"lists": [["a", "b", "c"], ["a", "c", "b"], ["a", "c", "d"]],
"count": [1, 2, 1],
}