GithubHelp home page GithubHelp logo

Comments (4)

orlp avatar orlp commented on August 17, 2024

We discussed it and we do not want this in Polars, sorry. We consider the with_columns expression fine.

Note that we already support broadcasting length-wise when multiplying by a series:

>>> df
shape: (3, 4)
┌─────┬─────┬─────┬─────┐
│ abcd   │
│ ------------ │
│ i64i64i64i64 │
╞═════╪═════╪═════╪═════╡
│ 14710  │
│ 25811  │
│ 36912  │
└─────┴─────┴─────┴─────┘
>>> df * pl.Series([1, 10, 100])
shape: (3, 4)
┌─────┬─────┬─────┬──────┐
│ abcd    │
│ ------------  │
│ i64i64i64i64  │
╞═════╪═════╪═════╪══════╡
│ 14710   │
│ 205080110  │
│ 3006009001200 │
└─────┴─────┴─────┴──────┘

Adding magic broadcasting column-wise would be very confusing in combination with the already existing above feature.

from polars.

u3Izx9ql7vW4 avatar u3Izx9ql7vW4 commented on August 17, 2024

@orlp Thanks for reviewing the request. I see where you're coming from, and it makes sense. What do you think about a named function instead, like how DataFrame has .sum() and .sum_horizontal()?

So the syntax could be something like:

# length-wise broadcast
df.multiply(pl.Series([1,2,3,4]))

# column-wise broadcast
df.multiply_horizontal(pl.Series([1,2,3,4))

I think that is more clear than

df.with_columns([pl.col(col) * multiplier for col, multiplier in zip(df.columns, [1,2,3,4])])

The reason I hope you would reconsider is that broadcasts along rows & columns is very common in both Numpy and Pandas. Users will be familiar with the concept via the axis parameter in those libraries. In Polars it looks like that parameter was removed, but the utility of broadcasting in multiple axes remains incredibly valuable.

Of course, column-wise broadcast could be trivially done by taking the transpose of the polars DataFrame and using your supplied syntax, but the documentation for the transpose method discourages its use:

This is a very expensive operation. Perhaps you can do it differently.

Given the current Polars API, my workaround is to convert to Pandas to perform that one line operation for readability purposes, before converting it back to Polars. I think this is a bit goofy but I probably won't be the only one doing this.

In any case, I hope there can be a more readable and concise solution than what we currently have.

from polars.

cmdlineluser avatar cmdlineluser commented on August 17, 2024

nth could perhaps tidy up the current with_columns approach a bit:

df.with_columns(pl.nth(n) * m for n, m in enumerate(multipliers))

from polars.

u3Izx9ql7vW4 avatar u3Izx9ql7vW4 commented on August 17, 2024

nth could perhaps tidy up the current with_columns approach a bit:

df.with_columns(pl.nth(n) * m for n, m in enumerate(multipliers))

Thanks that is better than before -- this could be good in the migration guide for Polars.

Having thought about it a bit more, if the Polars developers don't like sum_horizontal as it creates a precedence to have sister functions suffixed with _horizontal for other library functions, then I can see why the multiply_horizontal idea is disfavored.

For now I'll stick with Pandas' syntax, as a personal preference.

from polars.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.