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

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

Essentially the most fascinating issues concerning the new launch

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 enhancing 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 grew to become out there with pandas 2.0. We centered so much on constructing out the help for brand new options which can be anticipated to turn out to be the default with pandas 3.0. Let’s dig into what this implies for you. We are going to take a look at crucial enhancements intimately.

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

Avoiding NumPy object-dtype for string columns

One main ache level in pandas is the inefficient string illustration. This can be a subject that we labored on for fairly a while. The primary PyArrow backed string dtype grew to become 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 subject in additional depth in one of my previous 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, it will simply work.

You possibly can flip this selection on with:

pd.choices.future.infer_string = True

This habits will turn out to be the default in pandas 3.0, which signifies that string-columns would at all times be backed by PyArrow. You need to set up PyArrow to make use of this selection.

PyArrow has totally different habits than NumPy object dtype, which may make a ache to determine intimately. We carried out the string dtype that’s used for this selection to be suitable with NumPy sematics. It’s going to behave precisely the identical as NumPy object columns would. I encourage everybody to do this out!

Improved PyArrow help

We have now launched PyArrow backed DataFrame in pandas 2.0. One main purpose for us was to enhance the mixing inside pandas over the previous couple of months. We had been aiming to make the change from NumPy backed DataFrames as simple as attainable. One space that we centered on was fixing efficiency bottlenecks, since this triggered 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 perform that can be quicker now. We’re hopeful that the expertise with PyArrow backed DataFrames is a lot better now.

Copy-on-Write

Copy-on-Write was initially launched in pandas 1.5.0 and is predicted to turn out to be the default habits in pandas 3.0. Copy-on-Write gives a great expertise on pandas 2.0.x already. We had been largely centered on fixing recognized bugs and make it run quicker. I’d suggest to make use of this mode in manufacturing now. I wrote a sequence of weblog posts explaining what Copy-on-Write is and how it works. These weblog posts go into nice element and clarify how Copy-on-Write works internally and what you’ll be able to anticipate from it. This contains efficiency and habits.

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 every of your columns when you 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 have now a Collection with integers, which is able to lead to 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. This can be a main ache for lots of person. Object columns take up lots of reminiscence, calculations gained’t work anymore, efficiency degrades and plenty of different issues. It additionally added lots of particular casing internally to accomodate this stuff. Silent dtype adjustments in my DataFrame had been a serious annoyance for me up to now. This habits 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
suitable 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 once you wish to change your dtype, which provides a little bit of code however makes it simpler to observe for future builders.

This alteration impacts all dtypes, e.g. setting a float worth into an integer column may also increase.

Upgrading to the brand new model

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

pip set up -U pandas

Or:

mamba set up -c conda-forge pandas=2.1

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

Conclusion

We’ve checked out a few enhancements that may show you how to write extra environment friendly code. This contains 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 habits of pandas simpler to foretell within the subsequent main launch.

Thanks for studying. Be happy to succeed in out to share your ideas and suggestions.