What’s New in Pandas 2.1 | by Patrick Hoefler | Sep, 2023



Probably the most attention-grabbing issues in regards to the new launch

Patrick HoeflerTowards Data SciencePhotograph by Lukas W. on Unsplash

pandas 2.1 was launched on August thirtieth 2023. Let’s check out the issues this launch introduces and the way it will assist us bettering our pandas workloads. It features a bunch of enhancements and likewise a set of latest deprecations.

pandas 2.1 builds closely on the PyArrow integration that turned out there with pandas 2.0. We targeted rather a lot on constructing out the help for brand new options which can be anticipated to develop into the default with pandas 3.0. Let’s dig into what this implies for you. We’ll take a look at a very powerful enhancements intimately.

I’m a part of the pandas core group. I’m an open supply engineer for Coiled the place I work on Dask, together with bettering the pandas integration.

Avoiding NumPy object-dtype for string columns

One main ache level in pandas is the inefficient string illustration. It is a matter that we labored on for fairly a while. The primary PyArrow backed string dtype turned out there in pandas 1.3. It has the potential to cut back reminiscence utilization by round 70% and enhance the efficiency. I’ve explored this matter in additional depth in one in all my earlier posts, which incorporates reminiscence comparisons and efficiency measurements (tldr: it’s spectacular).

We’ve determined to introduce a brand new configuration possibility that may retailer all string columns in a PyArrow array. You don’t have to fret about casting string columns anymore, this can simply work.

You possibly can flip this feature on with:

pd.choices.future.infer_string = True

This conduct will develop into the default in pandas 3.0, which signifies that string-columns would all the time be backed by PyArrow. You need to set up PyArrow to make use of this feature.

PyArrow has totally different conduct than NumPy object dtype, which might make a ache to determine intimately. We applied the string dtype that’s used for this feature to be appropriate with NumPy sematics. It would behave precisely the identical as NumPy object columns would. I encourage everybody to do that out!

Improved PyArrow help

We’ve got launched PyArrow backed DataFrame in pandas 2.0. One main objective for us was to enhance the mixing inside pandas over the previous few months. We had been aiming to make the swap from NumPy backed DataFrames as simple as doable. One space that we targeted on was fixing efficiency bottlenecks, since this brought about sudden slowdowns earlier than.

Let’s take a look at an instance:

import pandas as pd
import numpy as np

df = pd.DataFrame(
“foo”: np.random.randint(1, 10, (1_000_000, )),
“bar”: np.random.randint(1, 100, (1_000_000,)),
}, dtype=”int64(pyarrow)”
grouped = df.groupby(“foo”)

Our DataFrame has 1 million rows and 10 teams. Let’s take a look at the efficiency on pandas 2.0.3 in comparison with pandas 2.1:

# pandas 2.0.3
10.6 ms ± 72.7 µs per loop (imply ± std. dev. of seven runs, 100 loops every)

# pandas 2.1.0
1.91 ms ± 3.16 µs per loop (imply ± std. dev. of seven runs, 1,000 loops every)

This specific instance is 5 instances quicker on the brand new model. merge is one other generally used operate that will probably be quicker now. We’re hopeful that the expertise with PyArrow backed DataFrames is significantly better now.


Copy-on-Write was initially launched in pandas 1.5.0 and is anticipated to develop into the default conduct in pandas 3.0. Copy-on-Write gives a superb expertise on pandas 2.0.x already. We had been principally targeted on fixing identified bugs and make it run quicker. I’d suggest to make use of this mode in manufacturing now. I wrote a collection of weblog posts explaining what Copy-on-Write is and the way it works. These weblog posts go into nice element and clarify how Copy-on-Write works internally and what you’ll be able to count on from it. This consists of efficiency and conduct.

We’ve seen that Copy-on-Write can enhance the efficiency of real-world workflows by over 50%.

Deprecating silent upcasting in setiten-like operations

Traditionally, pandas would silently change the dtype of one in all your columns for those who set an incompatible worth into it. Let’s take a look at an instance:

ser = pd.Collection((1, 2, 3))

0 1
1 2
2 3
dtype: int64

We’ve got a Collection with integers, which is able to end in integer dtype. Let’s set the letter “a” into the second row:

ser.iloc(1) = “a”

0 1
1 a
2 3
dtype: object

This adjustments the dtype of your Collection to object. Object is the one dtype that may maintain integers and strings. It is a main ache for lots of consumer. Object columns take up a whole lot of reminiscence, calculations received’t work anymore, efficiency degrades and lots of different issues. It additionally added a whole lot of particular casing internally to accomodate these items. Silent dtype adjustments in my DataFrame had been a serious annoyance for me up to now. This conduct is now deprecated and can increase a FutureWarning:

FutureWarning: Setting an merchandise of incompatible dtype is deprecated and can increase in a future
error of pandas. Worth ‘a’ has dtype incompatible with int64, please explicitly solid to a
appropriate dtype first.
ser.iloc(1) = “a”

Operations like our instance will increase an error in pandas 3.0. The dtypes of a DataFrames columns will keep constant throughout totally different operations. You’ll have to be express if you wish to change your dtype, which provides a little bit of code however makes it simpler to observe for future builders.

This variation impacts all dtypes, e.g. setting a float worth into an integer column will even increase.

Upgrading to the brand new model

You possibly can set up the brand new pandas model with:

pip set up -U pandas


mamba set up -c conda-forge pandas=2.1

This provides you with the brand new launch in your setting.


We’ve checked out a few enhancements that may show you how to write extra environment friendly code. This consists of efficiency enhancements, simpler opt-in into PyArrow backed string columns and additional enhancements for Copy-on-Write. We’ve additionally seen a deprecation that may make the conduct of pandas simpler to foretell within the subsequent main launch.

Thanks for studying. Be at liberty to achieve out to share your ideas and suggestions.


Supply hyperlink

What do you think?

Written by TechWithTrends

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings


Get the lowdown on the Webb Ellis Cup and win prizes with the official Rugby World Cup iPhone app


Google Search Console Insights Tip For Creating Fashionable Wanting Content material Is Not Official