pythondataframepython-polarspolars

Is there a way to vertically merge two polars.LazyFrames in Python?


I want to vertically merge two polars.LazyFrames in order to avoid collecting both LazyFrames beforehand, which is computationally expensive. I have tried extend(), concat(), and vstack() but none of them are implemented for LazyFrames. Maybe I am missing the point about LazyFrames by trying to perform this operation, but I am aware that join() works, which would also alter the dataframe's structure.


Solution

  • pl.concat can be used with LazyFrames:

    >>> lf = pl.LazyFrame({"x": [1, 2]})
    >>> pl.concat([lf, lf]).collect()
    shape: (4, 1)
    ┌─────┐
    │ x   │
    │ --- │
    │ i64 │
    ╞═════╡
    │ 1   │
    │ 2   │
    │ 1   │
    │ 2   │
    └─────┘