DEV Community

Cover image for Pandas is no longer the DataFrame King...
Daniel for BLST

Posted on

Pandas is no longer the DataFrame King...

Polars is

https://www.pola.rs/

Data analysis is a crucial aspect of many industries, including finance, healthcare, and technology. With the increasing amount of data generated every day, it's essential to have tools that can handle and manipulate large datasets with ease. That's where dataframe libraries come in handy. Two of the most popular dataframe libraries in Python are Pandas and PyPolars. In this article, we will compare the two and highlight the advantages of using PyPolars, particularly in terms of its higher read and write speed and lower RAM usage.

Pandas is a well-established library that has been around for more than a decade. It has a rich set of functionalities that make it a popular choice for data analysis. On the other hand, PyPolars is a relatively new library that was created to address some of the limitations of Pandas. PyPolars is designed to handle large datasets with much higher speed and efficiency, making it an attractive option for big data applications.

Recently, I was involved in a project that required refactoring the existing ETL pipelines and statistical models from Pandas to PyPolars. This was a significant project that involved dealing with large datasets and required efficient memory usage.

After the refactor, I was blown away by the performance increases we saw with PyPolars. The read and write speeds were much faster, and the processing time for large datasets was significantly reduced. This was a major win for our team as it allowed us to complete the project within the tight deadline we had.

However, the most significant difference I noticed was the decrease in RAM usage. Pandas has always been known to consume a lot of memory, and this was the main reason for the refactor. PyPolars uses a column-based data structure, which makes it possible to handle large datasets without consuming too much memory. In our tests, PyPolars used 10x less RAM than Pandas, which was a massive improvement. This not only made our pipelines more efficient but also reduced the costs associated with running them.

In terms of functionality, PyPolars is just as capable as Pandas. It offers a wide range of functionalities, including filtering, grouping, merging, and using the apply function, among others. This made it possible for us to complete the project without any significant changes to our existing code.

I set up a little Streamlit app to showcase how similar most of the syntax is! https://chainguns-streamlit-app-1z3ofs.streamlit.app/
Here are some code snippets from the app:

iris = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
iris_polars = pl.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")

st.write("Pandas DataFrame Shape:", iris.shape)
st.write("PyPolars DataFrame Shape:", iris_polars.shape)

st.header("DataFrame Operations")
st.write("We will perform some common dataframe operations on both Pandas and PyPolars dataframes")

st.write("Pandas head:")
st.write(iris.head())

st.write("PyPolars head:")
st.write(iris_polars.head())


st.write("Pandas describe:")
st.write(iris.describe())
st.write("PyPolars describe:")
st.write(iris_polars.describe())

st.header("Groupby Operations")
st.write("We will perform a groupby operation on both Pandas and PyPolars dataframes")

st.write("Pandas groupby mean:")
st.write(iris.groupby("species").mean())

st.write("PyPolars groupby mean:")
st.write(iris_polars.groupby("species").mean())
Enter fullscreen mode Exit fullscreen mode

I only had to slightly change the filter, the rest of the syntax is nearly identical, if you are familiar with writing SQL queries, you will feel write at home using polars.
In conclusion, my experience with PyPolars has been overwhelmingly positive. The massive performance increases and reduced RAM usage have made it a game-changer for our team. If you're looking for a dataframe library that can handle large datasets efficiently, I highly recommend PyPolars.

I hope you enjoyed my article, if you did be sure to like it and maybe check us out at BLST :)

Star our Github repo and join the discussion in our Discord channel!
Test your API for free now at BLST!

Oldest comments (0)