pythondjangopandaspython-polars

Polars Dataframe via Django Query


I am exploring a change from pandas to polars. I like what I see.

Currently, it is simple to get the data into Pandas.

cf = Cashflow.objects.filter(acct=acct).values()
df = pd.DataFrame(cf)

So I figured it would be a simple change - but this will not work for me.

df = pl.DataFrame(cf)

What is the difference between using a Django query and putting the data inside Polars?

Thank you.


Solution

  • You just need to check the input parameters of Polars and the output data type of the Django queryset. In Polars the pl.DataFrame() constructor expects a list of dictionaries or a list of other data structures. Also when you run Cashflow.objects.filter(acct=acct).values() in Django, the result is a queryset of dictionaries, where each dictionary represents a row of data. an example is provided below:

    <QuerySet [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]>
    

    So the only thing you need to do is to convert the resulting queryset into a list before passing it to Polars:

    cf_list = list(Cashflow.objects.filter(account=acct).values())
    
    df = pl.DataFrame(cf_list)