Skip to content

union

laktory.narwhals_ext.dataframe.union ¤

FUNCTION DESCRIPTION
union

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

union(self, others) ¤

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

PARAMETER DESCRIPTION
others

Other DataFrame(s)

TYPE: AnyFrame | list[AnyFrame]

Examples:

import narwhals as nw
import polars as pl

import laktory as lk  # noqa: F401

df0 = nw.from_native(
    pl.DataFrame(
        {
            "x": [0, 1],
        }
    )
)

df = df0.laktory.union(df0)
print(df)
'''
┌──────────────────┐
|Narwhals DataFrame|
|------------------|
|      | x |       |
|      |---|       |
|      | 0 |       |
|      | 1 |       |
|      | 0 |       |
|      | 1 |       |
└──────────────────┘
'''
Source code in laktory/narwhals_ext/dataframe/union.py
 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
def union(self, others: AnyFrame | list[AnyFrame]) -> AnyFrame:
    """
    Return a new DataFrame containing the union of rows in this and another
    DataFrame.

    Parameters
    ----------
    others:
        Other DataFrame(s)

    Examples
    --------
    ```py
    import narwhals as nw
    import polars as pl

    import laktory as lk  # noqa: F401

    df0 = nw.from_native(
        pl.DataFrame(
            {
                "x": [0, 1],
            }
        )
    )

    df = df0.laktory.union(df0)
    print(df)
    '''
    ┌──────────────────┐
    |Narwhals DataFrame|
    |------------------|
    |      | x |       |
    |      |---|       |
    |      | 0 |       |
    |      | 1 |       |
    |      | 0 |       |
    |      | 1 |       |
    └──────────────────┘
    '''
    ```
    """
    if not isinstance(others, (list, tuple, set)):
        others = [others]
    return nw.concat([self._df] + others)