These are the changes in pandas 1.3.0. See :ref:`release` for a full changelog including other versions of pandas.
{{ header }}
These are bug fixes that might have notable behavior changes.
Assigning values with DataFrame.__setitem__
now consistently assigns a new array, rather than mutating inplace (:issue:`33457`, :issue:`35271`, :issue:`35266`)
Previously, DataFrame.__setitem__
would sometimes operate inplace on the
underlying array, and sometimes assign a new array. Fixing this inconsistency
can have behavior-changing implications for workloads that relied on inplace
mutation. The two most common cases are creating a DataFrame
from an array
and slicing a DataFrame
.
Previous Behavior
The array would be mutated inplace for some dtypes, like NumPy's int64
dtype.
>>> import pandas as pd
>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> df = pd.DataFrame(a, columns=['a'])
>>> df['a'] = 0
>>> a # mutated inplace
array([0, 0, 0])
But not others, like :class:`Int64Dtype`.
>>> import pandas as pd
>>> import numpy as np
>>> a = pd.array([1, 2, 3], dtype="Int64")
>>> df = pd.DataFrame(a, columns=['a'])
>>> df['a'] = 0
>>> a # not mutated
<IntegerArray>
[1, 2, 3]
Length: 3, dtype: Int64
New Behavior
In pandas 1.3.0, DataFrame.__setitem__
consistently sets on a new array rather than
mutating the existing array inplace.
For NumPy's int64 dtype
.. ipython:: python import pandas as pd import numpy as np a = np.array([1, 2, 3]) df = pd.DataFrame(a, columns=['a']) df['a'] = 0 a # not mutated
For :class:`Int64Dtype`.
.. ipython:: python import pandas as pd import numpy as np a = pd.array([1, 2, 3], dtype="Int64") df = pd.DataFrame(a, columns=['a']) df['a'] = 0 a # not mutated
This also affects cases where a second Series
or DataFrame
is a view on a first DataFrame
.
df = pd.DataFrame({"A": [1, 2, 3]})
df2 = df[['A']]
df['A'] = np.array([0, 0, 0])
Previously, whether df2
was mutated depending on the dtype of the array being assigned to. Now, a new array is consistently assigned, so df2
is not mutated.
- Bug in :meth:`DataFrame.iloc.__setitem__` creating a new array instead of overwriting
Categorical
values in-place (:issue:`35417`)