Giter Site home page Giter Site logo

Comments (5)

cmdlineluser avatar cmdlineluser commented on August 18, 2024 1

I think .with_context existed as a workaround for the lack of a horizontal concat on LazyFrames.

That was recently added:

I recall reading in one of the previous issues that .with_context may end up being deprecated now?

from polars.

mcrumiller avatar mcrumiller commented on August 18, 2024 1

I recall reading in one of the previous issues that .with_context may end up being deprecated now?

I don't think it should, with_context allows for aggregate operations on different sized frames, which is nice:

>>> import polars as pl
>>> ldf1 = pl.LazyFrame({"a": [1, 2, 3]})
>>> ldf2 = pl.LazyFrame({"b": [1, 2, 3, 4]})
>>> ldf1.with_context(ldf2).select(pl.all().sum()).collect()
shape: (1, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 6   ┆ 10  │
└─────┴─────┘

from polars.

h4ck4l1 avatar h4ck4l1 commented on August 18, 2024

It makes no difference to me though as its only a matter of calling .lazy() when using .with_context. I am just curious.

from polars.

h4ck4l1 avatar h4ck4l1 commented on August 18, 2024

oh....I didn't know that. So it was a workaround.
bro doesn't .with_context have its own unique advantages?
let me give you an example where I am kinda struggling without .with_context on dataframes/lazyframes

If I want to get a mask of different dataframe and directly plot.

A = (
    pl.DataFrame({
        "id":np.arange(1000),
        "some_ranodm_strcol":np.random.choice(a=["a","b","c"],size=1000)
    })
)

B = (
    pl.DataFrame({
        "id":np.random.choice(1000,size=500,replace=False),
        "some_ranodm_strcol":np.random.choice(a=["a","b","c"],size=500)
    })
    .sort(by="id")
)
(
    A
    .lazy()
    .with_context(
        B.lazy().select(pl.all().suffix("_b"))
    )
    .select(
        pl.col("id"),
        pl.col("id").is_in(pl.col("id_b")).cast(pl.Int8).alias("new_col")
    )
    .collect()
    .plot.line(
        x="id",
        y="new_col"
    )
)

Is there a different alternative?. I learnt .with_context in udemy's course and its quite helpful when I want to use other dataframes columns to fast visualize(though they didn't teach that)
Both frames have different sizes. so with .with_context omits the necessity to select/extract series from both the frames and do operations on them.

This is why I was curious as to why eager frames don't have .with_context. Its such a unique and novel feature compared to pandas

from polars.

h4ck4l1 avatar h4ck4l1 commented on August 18, 2024

I recall reading in one of the previous issues that .with_context may end up being deprecated now?

I don't think it should, with_context allows for aggregate operations on different sized frames, which is nice:

>>> import polars as pl
>>> ldf1 = pl.LazyFrame({"a": [1, 2, 3]})
>>> ldf2 = pl.LazyFrame({"b": [1, 2, 3, 4]})
>>> ldf1.with_context(ldf2).select(pl.all().sum()).collect()
shape: (1, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 6   ┆ 10  │
└─────┴─────┘

Yes exactly. and .with_context should also be extended to pl.DataFrames if theres no downsides to it

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.