Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polars - to_dicts(). Is the order of the List of Dictionaries guaranteed?

I am leveraging the function .to_dicts() in my testing the resulting list of dictionaries has the same order as the dataframe.

df = pl.DataFrame(data={
    "name": ["Alice", "Bob", "Charlie"],
    "age": [30, 25, 35],
    "city": ["New York", "London", "Paris"],
}).sort(by=['name'], descending=True)

data = df.to_dicts()

for record in data:
    print(record)

Results in:

{'name': 'Charlie', 'age': 35, 'city': 'Paris'}
{'name': 'Bob', 'age': 25, 'city': 'London'}
{'name': 'Alice', 'age': 30, 'city': 'New York'}

Does anyone know if that is guaranteed to happen every time?

like image 422
Grant Culp Avatar asked Oct 30 '25 10:10

Grant Culp


1 Answers

TLDR. Yes.

This can be seen by inspecting this underlying rust function, which maps over the indices of the DataFrame in order.

like image 125
Hericks Avatar answered Nov 02 '25 01:11

Hericks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!