3

Is there a way to save Polars DataFrame into a database, MS SQL for example?

ConnectorX library doesn’t seem to have that option.

Dennis L
  • 33
  • 3
  • I don't know of a way, but you can always do `df.to_pandas().to_sql(...)` for now. – MYK Feb 02 '23 at 15:48
  • 2
    For **`polars>=0.16.10`** you can use `df.write_database()` [method](https://github.com/pola-rs/polars/releases/tag/py-0.16.10). – glebcom Mar 04 '23 at 08:05

2 Answers2

4

Polars exposes the write_database method on the DataFrame class.

ritchie46
  • 10,405
  • 1
  • 24
  • 43
2

Polars doesen't support direct writing to a database. You can proceed in two ways:

  1. Export the DataFrame in an intermediate format (such as .csv using .write_csv()), then import it into the database.
  2. Process it in memory: you can convert the DataFrame in a simpler data structure using .to_dicts(). The result will be a list of dictionaries, each of them containing a row in key/value format. At this point is easy to insert them into a database using SqlAlchemy or any specific library for your database of choice.
Liutprand
  • 527
  • 2
  • 8