Skip to content

Commit 20fc744

Browse files
authored
DEPR: invalid-for-dtype fill_value in unstack (pandas-dev#62289)
1 parent 567de07 commit 20fc744

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ Other Deprecations
659659
- Deprecated strings ``w``, ``d``, ``MIN``, ``MS``, ``US`` and ``NS`` denoting units in :class:`Timedelta` in favour of ``W``, ``D``, ``min``, ``ms``, ``us`` and ``ns`` (:issue:`59051`)
660660
- Deprecated the ``arg`` parameter of ``Series.map``; pass the added ``func`` argument instead. (:issue:`61260`)
661661
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
662+
- Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.unstack` and :meth:`DataFrame.unstack` (:issue:`12189`, :issue:`53868`)
662663

663664
.. ---------------------------------------------------------------------------
664665
.. _whatsnew_300.prior_deprecations:

pandas/core/reshape/reshape.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
from pandas._config.config import get_option
1414

1515
import pandas._libs.reshape as libreshape
16-
from pandas.errors import PerformanceWarning
16+
from pandas.errors import (
17+
Pandas4Warning,
18+
PerformanceWarning,
19+
)
1720
from pandas.util._decorators import cache_readonly
1821
from pandas.util._exceptions import find_stack_level
1922

@@ -28,7 +31,10 @@
2831
needs_i8_conversion,
2932
)
3033
from pandas.core.dtypes.dtypes import ExtensionDtype
31-
from pandas.core.dtypes.missing import notna
34+
from pandas.core.dtypes.missing import (
35+
isna,
36+
notna,
37+
)
3238

3339
import pandas.core.algorithms as algos
3440
from pandas.core.algorithms import (
@@ -298,7 +304,25 @@ def get_new_values(self, values, fill_value=None):
298304
new_values[:] = fill_value
299305
else:
300306
if not mask_all:
307+
old_dtype = dtype
301308
dtype, fill_value = maybe_promote(dtype, fill_value)
309+
if old_dtype != dtype:
310+
if old_dtype.kind not in "iub":
311+
warnings.warn(
312+
# GH#12189, GH#53868
313+
"Using a fill_value that cannot be held in the existing "
314+
"dtype is deprecated and will raise in a future version.",
315+
Pandas4Warning,
316+
stacklevel=find_stack_level(),
317+
)
318+
elif not isna(fill_value):
319+
warnings.warn(
320+
# GH#12189, GH#53868
321+
"Using a fill_value that cannot be held in the existing "
322+
"dtype is deprecated and will raise in a future version.",
323+
Pandas4Warning,
324+
stacklevel=find_stack_level(),
325+
)
302326
new_values = np.empty(result_shape, dtype=dtype)
303327
if not mask_all:
304328
new_values.fill(fill_value)

pandas/tests/frame/test_stack_unstack.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ def test_unstack_fill(self, future_stack):
105105
)
106106
tm.assert_frame_equal(result, expected)
107107

108-
# From a series with incorrect data type for fill_value
109-
result = data.unstack(fill_value=0.5)
108+
msg = (
109+
"Using a fill_value that cannot be held in the existing dtype is deprecated"
110+
)
111+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
112+
# From a series with incorrect data type for fill_value
113+
result = data.unstack(fill_value=0.5)
110114
expected = DataFrame(
111115
{"a": [1, 0.5, 5], "b": [2, 4, 0.5]}, index=["x", "y", "z"], dtype=float
112116
)
@@ -161,8 +165,12 @@ def test_unstack_fill_frame(self):
161165
expected["B"] = expected["B"].astype(np.float64)
162166
tm.assert_frame_equal(result, expected)
163167

164-
# From a dataframe with incorrect data type for fill_value
165-
result = df.unstack(fill_value=0.5)
168+
msg = (
169+
"Using a fill_value that cannot be held in the existing dtype is deprecated"
170+
)
171+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
172+
# From a dataframe with incorrect data type for fill_value
173+
result = df.unstack(fill_value=0.5)
166174

167175
rows = [[1, 3, 2, 4], [0.5, 5, 0.5, 6], [7, 0.5, 8, 0.5]]
168176
expected = DataFrame(rows, index=list("xyz"), dtype=float)

0 commit comments

Comments
 (0)