Comments (3)
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.
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.
That worked, I'll open a PR shortly!
from pandera.
Related Issues (20)
- Optional import hypotheses doesn't install hypothesis HOT 3
- Custom Check Methods don't support custom error (any more)
- Unexpected behavior when validating date objects. pandera=0.19.1
- Compatibility issues with Pandas HOT 3
- pandera not compatible with numpy 2.0 HOT 2
- `SchemaFieldNotFoundError` with custom check function if no alias is provided.
- Adding missing columns with a string default
- Scalar return for check in polars-backed model fails on validation with `lazy=True`
- Setting `coerce` on a column causes the column to be `required` when `required=False` HOT 1
- Support Data synthesis strategies for polars
- Polars checks not being evaluated correctly
- Pyinstaller build fails when using pydantic version 2.*
- `dataframe_parser`s that rename columns conflict with type coercion
- Wrong JSON output from `SchemaErrors.message`
- Is it possible to create a check function that accepts additional arguments?
- Why is pa.String returned as 'str' instead of 'string' when used with Columns? HOT 2
- BackendNotFoundError on databricks/pyspark cluster
- Example on how to use Decimal as dtype for a column
- Feat: Adding more pyarrow types to pandas engine
- Pydantic compatibility issue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandera.