Comments (1)
Feel free to adapt this:
def cast_str_bool(
*cols: str, replacer: Literal["lower", "upper", "capwords"] = "lower"
) -> Expr:
"""
Casts `cols` through `Int64` to `Boolean`, via Expr.`replace`.
Selection is via cs.`matches`, and can be applied transparently.
Parameters
----------
*cols
One or more column names, which are of type String
replacer
remapping strategy from _ to 1,0.
`lower`: 'true', 'false'
`upper`: 'TRUE', 'FALSE'
`capwords`: 'True', 'False'
"""
match replacer:
case "lower":
mapping = {
"true": 1,
"false": 0,
}
case "upper":
mapping = {
"TRUE": 1,
"FALSE": 0,
}
case "capwords":
mapping = {
"True": 1,
"False": 0,
}
case _:
msg = (
f"`replacer` must be one of {["lower", "upper", "capwords"]!r}\n"
f"but got {replacer!r}"
)
raise ValueError(msg)
return (
cs.matches(f"^{"|".join(cols)}$")
.replace(mapping)
.str.to_integer()
.cast(pl.Boolean)
)
from polars.
Related Issues (20)
- Deprecate `str.explode`
- `Array` columns not supported by `extend()` HOT 2
- Write support for Apache Iceberg HOT 2
- Linting error on `pl.read_csv(...)`: `Argument of type "IO[bytes]"` incompatible with `str | TextIO | BytesIO | Path | BinaryIO | bytes` HOT 4
- Pivot fail when one of the index columns is a list - regression from 0.20.6 HOT 11
- Improve documentation for floordiv
- read_csv issues a misleading warning when using non-utf8 encoding and glob pattern HOT 2
- len/count regression since 0.20.6 - 11x times slower in sample HOT 5
- df.assert_schema(expected_schema) HOT 5
- Allow not using cloudpickle in LazyFrame.serialize() HOT 1
- LazyFrame.deserialize() should document the security implications HOT 3
- `FromIterator` for `Series` should extend to `Option<String>` and `Option<&'a str>`
- Initialize `LazyFrame` from `LazyFrame` HOT 4
- Saving parquet to Google Cloud Storage with `df.write_parquet()` HOT 2
- Make `read_database` and `read_database_uri` consistent
- Incorrect description on read_csv_batched function HOT 1
- SQLContext window function misinterpretation when combining PARTITION BY and ORDER BY
- Missing documentation for how various formats are turned into polars dataframes HOT 1
- Stack overflow on joinon high cpu count machine HOT 2
- Native parquet reader coerces null string values to empty strings
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 polars.