Giter VIP home page Giter VIP logo

Comments (3)

tpaxman avatar tpaxman commented on September 23, 2024 1

Came across this after looking for info on type hints in Polars (via the issue you referenced). Off the top of my head, I don't know that there wouldn't be cases where the type resulting from certain operations would be unknown if the argument types are known (apart from maybe some uses of map_elements or operations on Object types, but haven't thought very far on this).

I think your enhancement idea is a worthwhile endeavor (though I admit I am not very familiar with the Rust code base myself). It did however get me thinking more about typing in Polars and has made me wonder: why are all expressions the same "type" anyway (i.e., Expr)?

It seems it would actually be useful to have multiple subclasses of Expr that are specific to each of the native types (e.g., something like StringExpr, Float64Expr, and so forth). This way, a chain of methods like:

pl.col('some_floats').cast(pl.String) would have a type that explicitly describes the types of its elements (e.g., StringExpr) which would allow direct introspection without the need for a meta accessor. What's more, you wouldn't be permitted to do something like pl.col('some_floats').cast(pl.String).round(2) because no such method would exist, thereby minimizing run-time errors maybe.

And of course, this would be a major improvement for type hinting with functions such as this:

def foo(a: pl.Expr, b: pl.Expr) -> pl.Expr:
    return pl.when(a.round(0) == 4, b.str.strip_chars()).then(200).otherwise(900)

Where it would be helpful to have a signature more like:

def foo(a: pl.Float64Expr, b: pl.StringExpr) -> pl.Int32Expr:

It may be that I am missing some critical piece that would show why this cannot be done, but from what I can tell, it seems like something that could help to clarify types (which seems to fit well with Polars' already excellent treatment of types and type consistency) and I would be interested in seeing something that at least takes things in this direction. Curious if you have any thoughts.

from polars.

butterlyn avatar butterlyn commented on September 23, 2024 1

Came across this after looking for info on type hints in Polars (via the issue you referenced). Off the top of my head, I don't know that there wouldn't be cases where the type resulting from certain operations would be unknown if the argument types are known (apart from maybe some uses of map_elements or operations on Object types, but haven't thought very far on this).

I think your enhancement idea is a worthwhile endeavor (though I admit I am not very familiar with the Rust code base myself). It did however get me thinking more about typing in Polars and has made me wonder: why are all expressions the same "type" anyway (i.e., Expr)?

It seems it would actually be useful to have multiple subclasses of Expr that are specific to each of the native types (e.g., something like StringExpr, Float64Expr, and so forth). This way, a chain of methods like:

pl.col('some_floats').cast(pl.String) would have a type that explicitly describes the types of its elements (e.g., StringExpr) which would allow direct introspection without the need for a meta accessor. What's more, you wouldn't be permitted to do something like pl.col('some_floats').cast(pl.String).round(2) because no such method would exist, thereby minimizing run-time errors maybe.

And of course, this would be a major improvement for type hinting with functions such as this:

def foo(a: pl.Expr, b: pl.Expr) -> pl.Expr:
    return pl.when(a.round(0) == 4, b.str.strip_chars()).then(200).otherwise(900)

Where it would be helpful to have a signature more like:

def foo(a: pl.Float64Expr, b: pl.StringExpr) -> pl.StringExpr:

It may be that I am missing some critical piece that would show why this cannot be done, but from what I can tell, it seems like something that could help to clarify types (which seems to fit well with Polars' already excellent treatment of types and type consistency) and I would be interested in seeing something that at least takes things in this direction. Curious if you have any thoughts.

Perhaps this is best raised in a separate issue? Though type hinting is a very important tool and this kind of feature would be great, having runtime access to column dtypes unlocks new capabilities that are otherwise impossible/impractical with the current Polars API.

from polars.

tpaxman avatar tpaxman commented on September 23, 2024

Thanks @butterlyn. I decided to write this out in more detail in #18082 and looking to get some feedback.

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.