pythonnumpyoperationpython-polars

Polars columns subtract order does not matter (apparently)


I would like to use polars, but when I try to subtract a 1x3 numpy array from three columns of the DataFrame. The problem is that is does not matter in which order the subtraction is applied:

import numpy as np
import polars as pl

# create polars dataframe:
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
df = pl.DataFrame(data, schema=['x', 'y', 'z']).with_columns(
    pl.all().cast(pl.Float64)
)

# subraction array:
arr = np.array([2, 5, 8], dtype=np.float64)

# subtract array from DataFrame
df.with_columns(
    pl.col('x') - arr[0],
    pl.col('y') - arr[1],
    pl.col('z') - arr[2],
)


"""
This one is correct, top row should be negative and bottom row positive
|    |   x |   y |   z |
|---:|----:|----:|----:|
|  0 |  -1 |  -1 |  -1 |
|  1 |   0 |   0 |   0 |
|  2 |   1 |   1 |   1 |
"""

df.with_columns(
    arr[0] - pl.col('x'),
    arr[1] - pl.col('y'),
    arr[2] - pl.col('z'),
)

"""
This one is incorrect. The top row should be positive and the bottom row should
be negative.
|    |   x |   y |   z |
|---:|----:|----:|----:|
|  0 |  -1 |  -1 |  -1 |
|  1 |   0 |   0 |   0 |
|  2 |   1 |   1 |   1 |
"""

Solution

  • Can't reproduce this, looks fine to me as of 0.16.5:

    In [57]: df.with_columns((
        ...:     pl.col('x') - arr[0],
        ...:     pl.col('y') - arr[1],
        ...:     pl.col('z') - arr[2],
        ...: ))
        ...:
    Out[57]:
    shape: (3, 3)
    ┌──────┬──────┬──────┐
    │ x    ┆ y    ┆ z    │
    │ ---  ┆ ---  ┆ ---  │
    │ f64  ┆ f64  ┆ f64  │
    ╞══════╪══════╪══════╡
    │ -1.0 ┆ -1.0 ┆ -1.0 │
    │ 0.0  ┆ 0.0  ┆ 0.0  │
    │ 1.0  ┆ 1.0  ┆ 1.0  │
    └──────┴──────┴──────┘
    
    In [58]: df.with_columns((
        ...:     arr[0] - pl.col('x'),
        ...:     arr[1] - pl.col('y'),
        ...:     arr[2] - pl.col('z'),
        ...: ))
    Out[58]:
    shape: (3, 3)
    ┌──────┬──────┬──────┐
    │ x    ┆ y    ┆ z    │
    │ ---  ┆ ---  ┆ ---  │
    │ f64  ┆ f64  ┆ f64  │
    ╞══════╪══════╪══════╡
    │ 1.0  ┆ 1.0  ┆ 1.0  │
    │ 0.0  ┆ 0.0  ┆ 0.0  │
    │ -1.0 ┆ -1.0 ┆ -1.0 │
    └──────┴──────┴──────┘