Skip to content

union

laktory.polars.dataframe.union ¤

Functions¤

union ¤

union(df, other)

Return a new DataFrame containing the union of rows in this and another DataFrame.

PARAMETER DESCRIPTION
df

Input DataFrame

TYPE: DataFrame

other

Other DataFrame

TYPE: DataFrame

Examples:

import laktory  # noqa: F401
import polars as pl

df0 = pl.DataFrame(
    {
        "symbol": ["AAPL", "AAPL"],
        "price": [200.0, 205.0],
        "tstamp": ["2023-09-01", "2023-09-02"],
    }
)

df = df0.laktory.union(df0)
print(df.glimpse(return_as_string=True))
'''
Rows: 4
Columns: 3
$ symbol <str> 'AAPL', 'AAPL', 'AAPL', 'AAPL'
$ price  <f64> 200.0, 205.0, 200.0, 205.0
$ tstamp <str> '2023-09-01', '2023-09-02', '2023-09-01', '2023-09-02'
'''
Source code in laktory/polars/dataframe/union.py
 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
def union(df: pl.DataFrame, other: pl.DataFrame) -> pl.DataFrame:
    """
    Return a new DataFrame containing the union of rows in this and another
    DataFrame.

    Parameters
    ----------
    df:
        Input DataFrame
    other:
        Other DataFrame

    Examples
    --------
    ```py
    import laktory  # noqa: F401
    import polars as pl

    df0 = pl.DataFrame(
        {
            "symbol": ["AAPL", "AAPL"],
            "price": [200.0, 205.0],
            "tstamp": ["2023-09-01", "2023-09-02"],
        }
    )

    df = df0.laktory.union(df0)
    print(df.glimpse(return_as_string=True))
    '''
    Rows: 4
    Columns: 3
    $ symbol <str> 'AAPL', 'AAPL', 'AAPL', 'AAPL'
    $ price  <f64> 200.0, 205.0, 200.0, 205.0
    $ tstamp <str> '2023-09-01', '2023-09-02', '2023-09-01', '2023-09-02'
    '''
    ```
    """
    return pl.concat([df, other])