-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.py
57 lines (40 loc) · 1.44 KB
/
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
# Authors: Thierry Moudiki
#
# License: BSD 3
import pandas as pd
import polars as pl
from ..utils import polars_to_pandas, pandas_to_polars
# just for 'completeness' of the interface
# already straightforward in pd
# df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'],
# 'value': np.random.randn(4)})
# df2 = pd.DataFrame({'key': ['B', 'D', 'D', 'E'],
# 'value': np.random.randn(4)})
# join(df1, df2, on='key')
# join(df1, df2, 'key', "left")
# join(df1, df2, 'key', "right")
# join(df1, df2, 'key', "outer")
def join(df1, df2, on=None, type_join="inner", **kwargs):
""" Join data frames.
Args:
df1: a data frame
a data frame
df2: a data frame
a data frame
on: str
joining column/criterion
type_join: str
type of join. Options are: "left", "right", "outer", "inner".
Default is "inner" join.
Examples:
https://github.com/thierrymoudiki/querier/tree/master/querier/demo
"""
if on is not None:
on_ = on.replace(" ", "").split(",")
if isinstance(df1, pl.DataFrame):
df1 = polars_to_pandas(df1)
df2 = polars_to_pandas(df2)
result = pd.merge(df1, df2, on=on_, how=type_join, **kwargs)
if isinstance(df1, pl.DataFrame):
return pandas_to_polars(result)
return result