Giter Site home page Giter Site logo

Custom DTypes With Polars about pandera HOT 3 CLOSED

Filimoa avatar Filimoa commented on June 11, 2024
Custom DTypes With Polars

from pandera.

Comments (3)

cosmicBboy avatar cosmicBboy commented on June 11, 2024

Looks like a bug in pandera.typing.Series... I think you can try just the bare type and it should work:

class Schema(pa.DataFrameModel):
    city: str
    price: LiteralFloat = pa.Field(coerce=True)

The correct implementation of the custom dtype is also:

from pandera.api.polars.types import PolarsData

@polars_engine.Engine.register_dtype
@dtypes.immutable
class LiteralFloat(polars_engine.Float64):  # 👈  inherit from polars_engine.Float64, not the polars dtype
    def coerce(self, polars_data: PolarsData) -> pl.LazyFrame:  # 👈 note the input and output signature
        """If comes across a string, remove commas and coerce it to a float. If it fails, return NaN."""
        return polars_data.lazyframe.with_columns(  # 👈 must return a lazyframe
            pl.col(polars_data.key)
            .str.replace(",", "")
            .cast(pl.Float64, strict=False)
        )

See the polars engine DataType implementation for details on the signatures of these methods:
https://github.com/unionai-oss/pandera/blob/main/pandera/engines/polars_engine.py#L91

I'll look into fixing the SchemaInitError: Invalid annotation 'price: pandera.typing.pandas.Series[__main__.LiteralFloat]' issue, if you can, would be great if the polars docs can be updated with an example of a custom datatype: https://github.com/unionai-oss/pandera/blob/main/docs/source/polars.md

from pandera.

cosmicBboy avatar cosmicBboy commented on June 11, 2024

I'll look into fixing the SchemaInitError: Invalid annotation 'price: pandera.typing.pandas.Series[main.LiteralFloat]' issue

So the whole Series[TYPE] syntax is only supported in the pandas DataFrameModel and will be deprecated in that API eventually... looking forward to new backends (in this case polars) the more concise bare type will be supported. I'll add a more informative error message here.

from pandera.

Filimoa avatar Filimoa commented on June 11, 2024

That worked, I'll open a PR shortly!

from pandera.

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.