# \[ANN\] First release of OCaml bindings to the Polars dataframe library

**URL:** <https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382>\
**Category:** Ecosystem\
**Tags:** announce\
**Created:** [November 7, 2023, 2:07pm UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382 "2023-11-07T14:07:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mt-caret](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mt-caret/32/4506_2.png) [@mt-caret](https://discuss.ocaml.org/u/mt-caret)\
**Post date:** [November 7, 2023, 2:07pm UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/1 "2023-11-07T14:07:59Z")

</div>

Hi everyone!

I’m excited to share a project I’ve been working on: [polars-ocaml](https://github.com/mt-caret/polars-ocaml), some OCaml bindings to the Polars dataframe library. If you’ve ever wanted to do data science or tabular data processing in OCaml, please consider trying this out!

[Polars](https://www.pola.rs/) is a quite performant dataframe library written in Rust, with an API that is built on top of the Apache Arrow format with a focus on performance, utilizing parallelism and SIMD to get pretty big speedups compared to regular records or libraries like pandas.

We’ve ported most of the examples in the Polars user guide to OCaml in the form of expect tests: [polars-ocaml/guide at main · mt-caret/polars-ocaml · GitHub](https://github.com/mt-caret/polars-ocaml/tree/main/guide)  
I encourage folks to take a look if you’re interested in seeing some examples of idiomatic usage of polars-ocaml; I think the labelled arguments and use of GADTs have made the API quite nice to work with!

# How to get it

We’ve just released the first version to opam, so you can install it with `opam install polars`. It also works with OCaml jupyter notebooks via [ocaml-jupyter](https://github.com/akabe/ocaml-jupyter):

 ![image](https://us1.discourse-cdn.com/flex020/uploads/ocaml/original/2X/6/61e1e66e5976caabed9aed094fe89640306d7b18.png)

# Contributing

If you find any issues or have any questions, feel free to comment or raise an issue on GitHub. While we’ve exposed a fair amount of polars functionality, there’s quite a lot more we haven’t gotten around to, so PRs are very welcome!

---

<div class="post-metadata">

**Author:** ![n4323](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@n4323](https://discuss.ocaml.org/u/n4323)\
**Post date:** [November 8, 2023, 9:52am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/2 "2023-11-08T09:52:32Z")

</div>

This looks super exciting!

I wonder how it interacts with standard OCaml types such as `array`, `bigarray` etc. Or is the general idea that all computations on Dataframes should be carried out within Polars functions?

Also, what would you recommend for interactive plotting?

Are you planning to blog post to show off the functionality at some point?

---

<div class="post-metadata">

**Author:** ![mt-caret](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mt-caret/32/4506_2.png) [@mt-caret](https://discuss.ocaml.org/u/mt-caret)\
**Post date:** [November 8, 2023, 10:07am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/3 "2023-11-08T10:07:53Z")

</div>

> Or is the general idea that all computations on Dataframes should be carried out within Polars functions?

That’s the idea, since then it’s possible to utilize the performance gains provided by the arrow memory layout, parallelism, SIMD, etc. You can go back and forth between OCaml value and Polars using functions like `Series.create` / `Series.to_list`.

> Also, what would you recommend for interactive plotting?

I haven’t personally tried it yet, but I think @laurent 's [GitHub - LaurentMazare/ocaml-matplotlib: Plotting for ocaml based on matplotlib.pyplot](https://github.com/LaurentMazare/ocaml-matplotlib) library should work with ocaml-jupyter so would probably be worth trying out.

> Are you planning to blog post to show off the functionality at some point?

That’s a good idea, I have a few more features I’m planning to implement so I’ll write something up once those things are done!

---

<div class="post-metadata">

**Author:** ![n4323](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@n4323](https://discuss.ocaml.org/u/n4323)\
**Post date:** [November 8, 2023, 10:47am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/4 "2023-11-08T10:47:02Z")

</div>

> [@mt-caret](#):
>
> You can go back and forth between OCaml value and Polars using functions like `Series.create` / `Series.to_list`.

It might be interesting to provide also generators, e.g. `Seq.t` which would be able to stream `Series` elements when a list would not fit in memory?

---

<div class="post-metadata">

**Author:** ![mt-caret](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mt-caret/32/4506_2.png) [@mt-caret](https://discuss.ocaml.org/u/mt-caret)\
**Post date:** [November 8, 2023, 10:59am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/6 "2023-11-08T10:59:27Z")

</div>

> [@mt-caret](#):
>
> That’s an interesting idea. I think one potential issue with getting generator-like things out of `Series.t` is that `Series.t` is mutable, so things can mutate under you. If you aren’t concerned about that, I think you can create something like this right now by using `Series.get` which lets you access a single element at a given index.

That’s an interesting idea. I think one potential issue with getting generator-like things out of `Series.t` is that `Series.t` is mutable, so things can mutate under you. If you aren’t concerned about that, I think you can create something like this right now by using `Series.get` which lets you access a single element at a given index.

---

<div class="post-metadata">

**Author:** ![edwin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/edwin/32/627_2.png) [@edwin](https://discuss.ocaml.org/u/edwin)\
**Post date:** [November 10, 2023, 12:01am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/7 "2023-11-10T00:01:22Z")

</div>

Interesting approach. How was your experience with OCaml ↔ Rust interop?  
I see it uses `ocaml-interop` which doesn’t require writing C stubs.

Also I notice that it is currently not available on OCaml 5, is that a limitation of `ocaml-interop`, or did you find some bugs with Rust interaction there?

---

<div class="post-metadata">

**Author:** ![mt-caret](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mt-caret/32/4506_2.png) [@mt-caret](https://discuss.ocaml.org/u/mt-caret)\
**Post date:** [November 10, 2023, 12:43pm UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/8 "2023-11-10T12:43:40Z")

</div>

> [@edwin](#):
>
> nteresting approach. How was your experience with OCaml ↔ Rust interop?  
> I see it uses `ocaml-interop` which doesn’t require writing C stubs.

Thanks! The experience was quite pleasant. I think @tizoc’s work here has made many of the pain points around writing correct and performant FFIs (specifically making sure that value lifetimes are properly handled), and I’m quite excited about the possibilities of this library.

> Also I notice that it is currently not available on OCaml 5, is that a limitation of `ocaml-interop`, or did you find some bugs with Rust interaction there?

Yes. I _think_ (but I may be wrong here) that it currently doesn’t support OCaml 5 yet (with [should the runtime be a thread-local variable? · Issue #42 · tizoc/ocaml-interop · GitHub](https://github.com/tizoc/ocaml-interop/issues/42), for example, being a blocker).

---

<div class="post-metadata">

**Author:** ![useada](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/useada/32/4627_2.png) [@useada](https://discuss.ocaml.org/u/useada)\
**Post date:** [November 19, 2023, 10:54am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/9 "2023-11-19T10:54:18Z")

</div>

Would you please make a windows ports? ex for dkml …

---

<div class="post-metadata">

**Author:** ![mt-caret](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mt-caret/32/4506_2.png) [@mt-caret](https://discuss.ocaml.org/u/mt-caret)\
**Post date:** [November 25, 2023, 4:07am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/10 "2023-11-25T04:07:52Z")

</div>

Sorry, I don’t have a Windows machine to test my code, but I’m happy to accept a PR that adds support for this!

---

<div class="post-metadata">

**Author:** ![jbeckford](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jbeckford/32/3027_2.png) [@jbeckford](https://discuss.ocaml.org/u/jbeckford)\
**Post date:** [November 25, 2023, 6:26am UTC](https://discuss.ocaml.org/t/ann-first-release-of-ocaml-bindings-to-the-polars-dataframe-library/13382/11 "2023-11-25T06:26:16Z")

</div>

Doubt that would be possible. `async` does not have a Windows implementation, among other things.
