Skip to content

Commit 54ae640

Browse files
committed
Merge branch 'main' into api-nan-vs-na
2 parents 9d4a112 + 46ede92 commit 54ae640

19 files changed

+94
-71
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ repos:
3232
exclude: ^pandas/tests
3333
args: [--select, "ANN001,ANN2", --fix-only, --exit-non-zero-on-fix]
3434
- id: ruff-format
35-
exclude: ^scripts|^pandas/tests/frame/test_query_eval.py
35+
exclude: ^pandas/tests/frame/test_query_eval.py
3636
- repo: https://github.com/jendrikseipp/vulture
3737
rev: v2.14
3838
hooks:

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ Other Deprecations
708708
- 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`)
709709
- Deprecated the ``arg`` parameter of ``Series.map``; pass the added ``func`` argument instead. (:issue:`61260`)
710710
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
711+
- 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`)
711712

712713
.. ---------------------------------------------------------------------------
713714
.. _whatsnew_300.prior_deprecations:

pandas/core/arrays/string_.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ def __from_arrow__(
344344
if self.storage == "pyarrow":
345345
from pandas.core.arrays.string_arrow import (
346346
ArrowStringArray,
347-
_chk_pyarrow_available,
347+
_check_pyarrow_available,
348348
)
349349

350-
_chk_pyarrow_available()
350+
_check_pyarrow_available()
351351

352352
if not pa.types.is_large_string(array.type):
353353
array = pc.cast(array, pa.large_string())

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)

scripts/check_for_inconsistent_pandas_namespace.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
from typing import NamedTuple
3131

3232
ERROR_MESSAGE = (
33-
"{path}:{lineno}:{col_offset}: "
34-
"Found both '{prefix}.{name}' and '{name}' in {path}"
33+
"{path}:{lineno}:{col_offset}: Found both '{prefix}.{name}' and '{name}' in {path}"
3534
)
3635

3736

scripts/check_test_naming.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
NOTE: if this finds a false positive, you can add the comment `# not a test` to the
99
class or function definition. Though hopefully that shouldn't be necessary.
1010
"""
11+
1112
from __future__ import annotations
1213

1314
import argparse

scripts/generate_pip_deps_from_conda.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
generated with this script:
1313
$ python scripts/generate_pip_deps_from_conda.py --compare
1414
"""
15+
1516
import argparse
1617
import pathlib
1718
import re

scripts/pandas_errors_documented.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
pre-commit run pandas-errors-documented --all-files
88
"""
9+
910
from __future__ import annotations
1011

1112
import argparse

scripts/sort_whatsnew_note.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
2424
pre-commit run sort-whatsnew-items --all-files
2525
"""
26+
2627
from __future__ import annotations
2728

2829
import argparse

0 commit comments

Comments
 (0)