<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ben Pinner on Medium]]></title>
        <description><![CDATA[Stories by Ben Pinner on Medium]]></description>
        <link>https://medium.com/@benpinner1997?source=rss-189b0343cd90------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ifHj_1b7-B9XeNz2qfvWdw.png</url>
            <title>Stories by Ben Pinner on Medium</title>
            <link>https://medium.com/@benpinner1997?source=rss-189b0343cd90------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 17:55:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@benpinner1997/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Data Processing: Pandas vs PySpark vs Polars]]></title>
            <link>https://medium.com/@benpinner1997/data-processing-pandas-vs-pyspark-vs-polars-fc1cdcb28725?source=rss-189b0343cd90------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc1cdcb28725</guid>
            <category><![CDATA[performance-testing]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[etl-pipeline]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Ben Pinner]]></dc:creator>
            <pubDate>Sun, 16 Jul 2023 10:56:30 GMT</pubDate>
            <atom:updated>2023-07-16T10:56:30.713Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*1qehD8muedoXvTWH4XllQg.png" /></figure><p><strong>TL;DR</strong> I write an ETL process in 3 different libraries (Polars, Pandas and PySpark) and run it against datasets of varying sizes to compare the results. I find that PySpark is clearly suited for Big Data and Polars is a much faster alternative to Pandas, but I question if the improvement warrants replacing Pandas.</p><p>The team I work on uses a mix of Pandas and PySpark for handling data. However, I recently came to the conclusion PySpark was not suitable for the sizes of data we’re using it for, it is intended for big data (Terabytes). This is mainly because PySpark comes with quite a bit of overhead that seriously slows it down when handling small/medium data, but is negligible when handling Big Data.</p><p>This got me thinking about one of the main bottlenecks in our system, a matching algorithm written using Pandas. It makes sense that this algorithm is a bottleneck, it’s doing the heavy lifting. But I did wonder — is Pandas so popular because it’s the best tool for the job, or because it’s been around for a while, has a strong ecosystem, and is easy to learn? Also, what other options are out there?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*DXanrLaGQ-g8PLiyx_SimA.png" /><figcaption>I don’t know what I expected to be honest</figcaption></figure><h3>New kid on the block</h3><p>After some research, I found Polars — it’s an apparently “blazingly fast DataFrame” library created just 2 years ago that seems to take some of the strengths of PySpark and Pandas. Polars is available as a Python library but is written in Rust, which is a bonus for me because I love Rust. I’ll avoid getting too technical as I want this post to be accessible, but I will mention that Polars gives engineers the ability to use it in Eager or Lazy mode, which I’ll briefly explain below.</p><p>In Eager mode it reads your code line-by-line and operates on the data as it goes, in Lazy mode, it will build a plan from the operations you’ve written, and then the engineer can tell Polars to optimise this plan before executing.</p><p>So, in simple terms, Eager mode is like cooking as you go, while Lazy mode is like planning a recipe in advance with the help of a chef, making sure everything is optimized for maximum efficiency.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*DfPa8psGplXZ8F18hwN93w.gif" /><figcaption><em>This lazy polar bear does not seem blazingly fast</em></figcaption></figure><h3>The Results</h3><p>Safe to say I was curious how these three libraries would compare when operating on data of different sizes. So I replicated one of my team’s ETL processes in Pandas, PySpark, and twice in Polars — once for Lazy mode, and once for Eager mode. I then ran this code and recorded the total time taken 50 times for each size of data: 10,000 rows, 100,000 rows, 600,000 rows, and 2.5 million rows. I captured the mean and generated a chart showing how long each library took to process the different sizes of data.</p><p><strong>10,000 Rows</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OQuSmTw8I6cRXrEOjmAZcg.png" /></figure><p>Woah!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*ppAKY6uCrQDx3Pqiwxv8Aw.gif" /></figure><p>As you can see, unsurprisingly, PySpark clearly isn’t a contender at this point, in the interest of actually being able to see the bars for Polars and Pandas, let’s exclude it until it can compete.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I5zekfrwfQCYa4xSrcTprA.png" /></figure><p><strong>100,000 Rows</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CsYVRQvuFYKKjrPTeVz1rg.png" /></figure><p><strong>600,000 Rows</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DkRJHHmRy8UjuJum5FNukA.png" /></figure><p><strong>2,500,000 Rows</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2B5p5HplaNSbB2irlPtCFw.png" /></figure><h3>What does it mean?</h3><p>I’ll start off with the most simple comparisons. Polars’ Lazy mode was around 1.15–1.7x faster than Eager mode, I’m sure this will vary for different operations — maybe a longer ETL process will give Lazy mode the chance to optimise more, or maybe not. Safe to say, this is a small, but nice performance gain.</p><p>What impressed me the most is the improvement Polars’ Lazy code makes over the Pandas code, Polars showed it could be anywhere between 9.4–22.6x faster than Pandas for this example. What’s more interesting is if we ignore the smallest dataset (10,000 rows), that range changes to 18.3–22.6x faster, perhaps the optimisations Polars has over Pandas are more effective for larger datasets.</p><p>The total processing time of Pandas actually increased linearly with data size, whereas the times for Polars didn’t increase so consistently, and in fact Polars generally seemed quite inconsistent. Before I started recording results, I regularly saw Polars process the largest dataset in 0.8s — under half the average time it eventually achieved — I don’t know why this is. When I ran these benchmarks it was a very hot day by English standards, and I was running this code 200 times in quick succession, so maybe that affected Polars’ performance, but why didn’t I see Pandas and PySpark take a similar hit? I don’t know, however when it came to comparing Polars to the other two, these were the results and after a couple of re-runs I wasn’t going to keep re-running until I got the results I expect.</p><p>Now let’s segue from the lack of consistency I found in Polars, to the Queen of Consistency.</p><p>For all data sizes, PySpark ran in 7–10 seconds, and I don’t know whether to consider that impressive or not. The problem with PySpark at this scale is the aforementioned overhead. There are two main reasons for this overhead: starting a Spark Session, and partitioning data. When you run PySpark code, it must first start a Spark Session, which — at a high level — launches a Java Virtual Machine, and launches a Session within that. PySpark then partitions the datasets it loads into lots of smaller datasets, 200 by default. Each of these processes took 3.5–5 seconds, meaning that before PySpark could even start processing data, it had to first spend 7–10 seconds preparing to process that data. Wait, but didn’t PySpark take 7–10 seconds to process it’s data? Yes, that’s because after this setup, PySpark only took 230ms-320ms to handle each of the datasets. Now that’s impressive!</p><h3>But what about Big Data?</h3><p>You may have noticed that I mentioned PySpark is designed for Big Data, Terabytes of data. But the largest dataset we’ve benchmarked against comes nowhere near that. That doesn’t seem fair, right?</p><p>I knew I couldn’t run this code on Big Data locally, as I don’t have enough space for a Terabyte of data. So I’d like to run these benchmarks in AWS Glue. I’d really like to see how Polars and PySpark would compare at these sizes, but I think we can extrapolate from the results we already have at smaller sizes of data to predict which of the two would be faster at processing Big Data.</p><p>Given PySpark’s initial setup time doesn’t noticeably increase as the size of the data it’s processing increases, and the actual processing time is rapid, I think it’s safe to say that when handling a Terabyte of data, PySpark would have really begun to shine. It’s clear in the graphs that, while fast, Polars was beginning to slow with bigger datasets, and so if this trend of Polars slowing and PySpark barely slowing at all continued, I can see PySpark blowing Polars out of the water at handling Big Data.</p><h3>Conclusion</h3><p>In an industry <a href="https://survey.stackoverflow.co/2022/#section-most-popular-technologies-other-frameworks-and-libraries">dominated by Pandas and PySpark</a>, it’s been interesting to see what else is out there.</p><p>First off, I think it’s obvious that PySpark is not suited to data sizes below a few Gigabytes, its setup time makes it incredibly slow. But on the flip side, this setup time and other tricks are what make PySpark incredibly fast at higher data sizes, and while I wasn’t able to directly prove this, there are those out there that have.</p><p>At first it could appear that Pandas no longer has any use case — it is beat by Polars by a considerable margin at every turn. It is possible to optimise Pandas, but I felt it was important to compare these libraries out of the box — either way, from what I can tell, these optimisations still don’t seem to bring it on par with Polars.</p><p>So is Pandas in danger? No, it’s an incredibly popular library that will continue to propagate to newcomers through word of mouth alone. Should it be in danger? I’m not sure. At big data sizes it’s clear PySpark beats Pandas, and Polars is a lot faster seemingly at all sizes of data. But before you start reaching gigabytes of data, it seems the difference between Polars and Pandas isn’t too substantial, especially not enough to learn a new library and replace an integral part of your data tech stack. However, if your ETL process is being run many times in a day, this may provide a reasonable time and cost optimisation.</p><p>I’m honestly biased towards Polars, but while it is impressive, it can also be hard to argue that people should replace Pandas with it. Pandas has a strong ecosystem, any error or problem you encounter with Pandas will have been solved 10 or 100 times over online, whereas I came across errors in Polars that I couldn’t find mentioned anywhere online. There’s a plethora of resources for learning Pandas, and it’s a very mature library, Polars isn’t.</p><p>However, I’ll keep using Polars, and I don’t like mentioning Pandas popularity as a reason not to use Polars. I hate stagnation, I think stagnation isn’t healthy — as one of my favourite quotes from my favourite sci-fi story “Dune” states “stagnation is death”. So yes, I think Polars should be adopted on mass, and if it is, maybe that will force Pandas and the people that maintain and update it to adapt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*o7ZqMZRFjWgKXpQLKq9A1w.gif" /></figure><h3>What’s next?</h3><p>I ended up timeboxing this investigation, because I could already feel this blog bloating. There is, however, much more I plan on investigating:</p><p>I’m eager to put more time into benchmarking in AWS Glue, in order to get concrete results for Big Data. Also, Glue is the environment that my team’s ETL processes run in, and so it would be useful for us to see how each library performs in this environment, especially since Glue is intended to be used with PySpark. I’m also curious as to whether Glue would provide more consistent results than my dev machine.</p><p>If I benchmark these libraries in more sizes of data, I could have enough data points to see trends in a line plot of these results, and could also potentially run a regression model to predict how each of the libraries would run at sizes I haven’t benchmarked.</p><p>I have only compared how these libraries handle a flat data structure. I did try loading a nested data structure in Polars, but I got an error back and can’t find anyone online that’s had the same issue — I mentioned this above as a potential teething problem when learning Polars, as it doesn’t have a mature ecosystem of developers yet. I’d like to put more time into solving this, so I can compare how the libraries handle this use case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc1cdcb28725" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>