Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there an efficient way in Polars to reinterpret a single byte in a pl.Binary (or string) as an int?

With pure Python, it is relatively simple to do ord(A) == 65, and as a slow Polars method, it is possible to do:

pl.Series(["A", "B", "C"]).cast(pl.Binary).map_elements(lambda x: ord(x)) == pl.Series([65,66,67])
pl.Series(["A", "B", "C"]).map_elements(lambda x: ord(x)) == pl.Series([65,66,67])

Is there a method (outside of writing a rust implementation, which would be simple, but potentially needless) to do this efficiently?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could cast to list of pl.UInt8 as:

pl.Series(["A", "B", "C"]).cast(pl.Binary).cast(pl.List(pl.UInt8))

Subsequently, you can use the methods available under the expr.list attribute to work with the list.

Is that good enough for your use-case?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading