Search code examples
pythonpandaspython-polars

Polars - AttributeError: 'ArrowExtensionArray' object has no attribute 'to_pydatetime'


I am trying to push dates to the SQLite database using Polars' write_database method:

import polars as pl
from datetime import date
import sqlite3

dates_dt = [date(2000, 1, 1), date(2000, 1, 2), date(2000, 1, 3)]
df = pl.DataFrame({"date": dates_dt})

conn = sqlite3.connect("test.db")
df.write_database("test_table", "sqlite:///test.db", if_table_exists="replace")
conn.close()

However, I got the following error:

AttributeError: 'ArrowExtensionArray' object has no attribute 'to_pydatetime'

The same problem occurs if date strings are converted to a Date column:

date_str = ["2000-01-01", "2000-01-02", "2000-01-03"]
df = pl.DataFrame({"date": date_str})
df = df.with_columns(pl.col("date").str.to_date("%Y-%m-%d"))

Solution

  • There was a bug in the pd.to_pandas function which was fixed since the pandas 2.1.0 version. Polars throws an error when writing a date or date time object if the pandas' version is not up-to-date.

    Therefore, the version needs to be updated:

    pip install --upgrade pandas
    

    Verify that the version is greater than 2.1.0 with the pip list | grep pandas command.

    References: