Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polars equivalent of numpy.tile

df = pl. DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})

print(df)

shape: (3, 2)
┌──────┬──────┐
│ col1 ┆ col2 │
│ ---  ┆ ---  │
│ i64  ┆ i64  │
╞══════╪══════╡
│ 1    ┆ 4    │
│ 2    ┆ 5    │
│ 3    ┆ 6    │
└──────┴──────┘

I am looking for the polars equivalent of numpy.tile.
Something along the line such as df.tile(2) or df.select(pl.all().tile(2)).

The expected result should look like this:

shape: (6, 2)
┌──────┬──────┐
│ col1 ┆ col2 │
│ ---  ┆ ---  │
│ i64  ┆ i64  │
╞══════╪══════╡
│ 1    ┆ 4    │
│ 2    ┆ 5    │
│ 3    ┆ 6    │
│ 1    ┆ 4    │
│ 2    ┆ 5    │
│ 3    ┆ 6    │
└──────┴──────┘
like image 398
Andi Avatar asked Jan 19 '26 08:01

Andi


1 Answers

You could concat several times the same DataFrame:

out = pl.concat([df]*2)

# for contiguous memory
out = pl.concat([df]*2, rechunk=True)

Or, for just 2 repeats vstack:

out = df.vstack(df)

Alternatively, using Expr.repeat_by + explode:

N = 2

(df.with_columns(pl.all().repeat_by(pl.lit(N)), pl.int_ranges(pl.lit(N)))
   .explode(pl.all())
   .sort(by='literal', maintain_order=True).drop('literal')
)

Output:

┌──────┬──────┐
│ col1 ┆ col2 │
│ ---  ┆ ---  │
│ i64  ┆ i64  │
╞══════╪══════╡
│ 1    ┆ 4    │
│ 2    ┆ 5    │
│ 3    ┆ 6    │
│ 1    ┆ 4    │
│ 2    ┆ 5    │
│ 3    ┆ 6    │
└──────┴──────┘
like image 197
mozway Avatar answered Jan 21 '26 01:01

mozway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!