Skip to content

Commit b0ffccd

Browse files
authored
DEPR: make_block (#56422)
* DEPR: make_block * lint fixup
1 parent feacb6f commit b0ffccd

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ Other Deprecations
432432
^^^^^^^^^^^^^^^^^^
433433
- Changed :meth:`Timedelta.resolution_string` to return ``h``, ``min``, ``s``, ``ms``, ``us``, and ``ns`` instead of ``H``, ``T``, ``S``, ``L``, ``U``, and ``N``, for compatibility with respective deprecations in frequency aliases (:issue:`52536`)
434434
- Deprecated :func:`pandas.api.types.is_interval` and :func:`pandas.api.types.is_period`, use ``isinstance(obj, pd.Interval)`` and ``isinstance(obj, pd.Period)`` instead (:issue:`55264`)
435+
- Deprecated :func:`pd.core.internals.api.make_block`, use public APIs instead (:issue:`40226`)
435436
- Deprecated :func:`read_gbq` and :meth:`DataFrame.to_gbq`. Use ``pandas_gbq.read_gbq`` and ``pandas_gbq.to_gbq`` instead https://pandas-gbq.readthedocs.io/en/latest/api.html (:issue:`55525`)
436437
- Deprecated :meth:`.DataFrameGroupBy.fillna` and :meth:`.SeriesGroupBy.fillna`; use :meth:`.DataFrameGroupBy.ffill`, :meth:`.DataFrameGroupBy.bfill` for forward and backward filling or :meth:`.DataFrame.fillna` to fill with a single value (or the Series equivalents) (:issue:`55718`)
437438
- Deprecated :meth:`Index.format`, use ``index.astype(str)`` or ``index.map(formatter)`` instead (:issue:`55413`)
@@ -484,7 +485,6 @@ Other Deprecations
484485
- Deprecated the extension test classes ``BaseNoReduceTests``, ``BaseBooleanReduceTests``, and ``BaseNumericReduceTests``, use ``BaseReduceTests`` instead (:issue:`54663`)
485486
- Deprecated the option ``mode.data_manager`` and the ``ArrayManager``; only the ``BlockManager`` will be available in future versions (:issue:`55043`)
486487
- Deprecated the previous implementation of :class:`DataFrame.stack`; specify ``future_stack=True`` to adopt the future version (:issue:`53515`)
487-
-
488488

489489
.. ---------------------------------------------------------------------------
490490
.. _whatsnew_220.performance:

pandas/core/internals/api.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
from __future__ import annotations
1010

1111
from typing import TYPE_CHECKING
12+
import warnings
1213

1314
import numpy as np
1415

1516
from pandas._libs.internals import BlockPlacement
17+
from pandas.util._exceptions import find_stack_level
1618

1719
from pandas.core.dtypes.common import pandas_dtype
1820
from pandas.core.dtypes.dtypes import (
@@ -50,6 +52,14 @@ def make_block(
5052
- Block.make_block_same_class
5153
- Block.__init__
5254
"""
55+
warnings.warn(
56+
# GH#40226
57+
"make_block is deprecated and will be removed in a future version. "
58+
"Use public APIs instead.",
59+
DeprecationWarning,
60+
stacklevel=find_stack_level(),
61+
)
62+
5363
if dtype is not None:
5464
dtype = pandas_dtype(dtype)
5565

@@ -113,7 +123,6 @@ def maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int
113123

114124
def __getattr__(name: str):
115125
# GH#55139
116-
import warnings
117126

118127
if name in [
119128
"Block",

pandas/tests/internals/test_api.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def test_deprecations(name):
6868
def test_make_block_2d_with_dti():
6969
# GH#41168
7070
dti = pd.date_range("2012", periods=3, tz="UTC")
71-
blk = api.make_block(dti, placement=[0])
71+
msg = "make_block is deprecated"
72+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
73+
blk = api.make_block(dti, placement=[0])
7274

7375
assert blk.shape == (1, 3)
7476
assert blk.values.shape == (1, 3)

pandas/tests/internals/test_internals.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -1383,9 +1383,11 @@ def test_validate_ndim():
13831383
values = np.array([1.0, 2.0])
13841384
placement = BlockPlacement(slice(2))
13851385
msg = r"Wrong number of dimensions. values.ndim != ndim \[1 != 2\]"
1386+
depr_msg = "make_block is deprecated"
13861387

13871388
with pytest.raises(ValueError, match=msg):
1388-
make_block(values, placement, ndim=2)
1389+
with tm.assert_produces_warning(DeprecationWarning, match=depr_msg):
1390+
make_block(values, placement, ndim=2)
13891391

13901392

13911393
def test_block_shape():
@@ -1400,23 +1402,29 @@ def test_make_block_no_pandas_array(block_maker):
14001402
# https://github.com/pandas-dev/pandas/pull/24866
14011403
arr = pd.arrays.NumpyExtensionArray(np.array([1, 2]))
14021404

1405+
warn = None if block_maker is not make_block else DeprecationWarning
1406+
msg = "make_block is deprecated and will be removed in a future version"
1407+
14031408
# NumpyExtensionArray, no dtype
1404-
result = block_maker(arr, BlockPlacement(slice(len(arr))), ndim=arr.ndim)
1409+
with tm.assert_produces_warning(warn, match=msg):
1410+
result = block_maker(arr, BlockPlacement(slice(len(arr))), ndim=arr.ndim)
14051411
assert result.dtype.kind in ["i", "u"]
14061412

14071413
if block_maker is make_block:
14081414
# new_block requires caller to unwrap NumpyExtensionArray
14091415
assert result.is_extension is False
14101416

14111417
# NumpyExtensionArray, NumpyEADtype
1412-
result = block_maker(arr, slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim)
1418+
with tm.assert_produces_warning(warn, match=msg):
1419+
result = block_maker(arr, slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim)
14131420
assert result.dtype.kind in ["i", "u"]
14141421
assert result.is_extension is False
14151422

14161423
# new_block no longer taked dtype keyword
14171424
# ndarray, NumpyEADtype
1418-
result = block_maker(
1419-
arr.to_numpy(), slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim
1420-
)
1425+
with tm.assert_produces_warning(warn, match=msg):
1426+
result = block_maker(
1427+
arr.to_numpy(), slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim
1428+
)
14211429
assert result.dtype.kind in ["i", "u"]
14221430
assert result.is_extension is False

pandas/tests/io/parser/common/test_chunksize.py

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def test_chunks_have_consistent_numerical_type(all_parsers, monkeypatch):
233233
assert result.a.dtype == float
234234

235235

236+
@pytest.mark.filterwarnings("ignore:make_block is deprecated:FutureWarning")
236237
def test_warn_if_chunks_have_mismatched_type(all_parsers):
237238
warning_type = None
238239
parser = all_parsers

pandas/tests/io/parser/test_parse_dates.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333

3434
from pandas.io.parsers import read_csv
3535

36-
pytestmark = pytest.mark.filterwarnings(
37-
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
38-
)
36+
pytestmark = [
37+
pytest.mark.filterwarnings(
38+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
39+
),
40+
pytest.mark.filterwarnings("ignore:make_block is deprecated:DeprecationWarning"),
41+
]
3942

4043
xfail_pyarrow = pytest.mark.usefixtures("pyarrow_xfail")
4144
skip_pyarrow = pytest.mark.usefixtures("pyarrow_skip")

0 commit comments

Comments
 (0)