Giter Site home page Giter Site logo

Comments (5)

JulianCologne avatar JulianCologne commented on July 19, 2024

yes, agree!
Have already shared by idea about something more general .assert / .expect here: #11064 (comment)

Would love to see:

.assert(...) or .expect

  • assert that some conditions are met like: datatypes, max < x, min > 0, schema = ...
  • with configuration options to turn on for "testing" (full evalutation) or "production" (ignore assertions)
  • df.select(...).assert(col(x)>0, pl.max(y) < 100).with_columns(...).assert(...).group_by...

.inspect(...)

  • allow printing / debugung current df values
  • df.select(...).with_columns(...).inspect(lambda df: print(df.schema)).select(...).inspect(lambda df: print(df.describe()).group_by...

from polars.

KDruzhkin avatar KDruzhkin commented on July 19, 2024

Interesting.

But schemas are different from other properties. You can get the schema of a LazyFrame without collecting it.

The more general assert and inspect methods of LazyFrames would only add delayed actions. (Still useful.)

from polars.

KDruzhkin avatar KDruzhkin commented on July 19, 2024

Wrote #16311 specifically about dropping/forgetting/ignoring values that are used only for assertions.

from polars.

stinodego avatar stinodego commented on July 19, 2024

You can already do this using pipe:

import polars as pl

def assert_schema(
    lf: pl.LazyFrame, schema: dict[str, pl.PolarsDataType]
) -> pl.LazyFrame:
    if lf.schema != schema:
        msg = (
            "Wrong LazyFrame schema:\n"
            f"• expected: '{schema}',\n"
            f"• observed: '{dict(lf.schema)}'."
        )
        raise AssertionError(msg)
    return lf

lf = pl.LazyFrame({"a": [1, 2, 3], "b": [4.0, 5.0, 6.0]})
lf.select("a").pipe(assert_schema, schema={"a": pl.Float64})
AssertionError: Wrong LazyFrame schema:
• expected: '{'a': Float64}',
• observed: '{'a': Int64}'.

I don't think this is worth adding to the Polars API.

For a more general 'assertion / raising' utility, please see #11064

from polars.

KDruzhkin avatar KDruzhkin commented on July 19, 2024

You can already do this using pipe:

Nice. I somehow missed pipe.

Maybe, it deserves a more prominent place in the docs? The user guide mentions it only in passing (Pipe littering: "Don't").

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.