Skip to content

Commit 6c321bb

Browse files
authored
DEPR: make_block (#57754)
1 parent 76c7274 commit 6c321bb

File tree

8 files changed

+79
-26
lines changed

8 files changed

+79
-26
lines changed

doc/source/whatsnew/v3.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ starting with 3.0, so it can be safely removed from your code.
263263
Other Deprecations
264264
^^^^^^^^^^^^^^^^^^
265265

266+
- Deprecated :func:`core.internals.api.make_block`, use public APIs instead (:issue:`56815`)
266267
- Deprecated :meth:`.DataFrameGroupby.corrwith` (:issue:`57158`)
267268
- Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`)
268269
- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`)
@@ -272,7 +273,6 @@ Other Deprecations
272273
- Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``datetime.timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties. (:issue:`57463`)
273274
- Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`)
274275
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
275-
-
276276

277277
.. ---------------------------------------------------------------------------
278278
.. _whatsnew_300.prior_deprecations:

pandas/core/internals/api.py

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from __future__ import annotations
1111

1212
from typing import TYPE_CHECKING
13+
import warnings
1314

1415
import numpy as np
1516

@@ -87,6 +88,15 @@ def make_block(
8788
- Block.make_block_same_class
8889
- Block.__init__
8990
"""
91+
warnings.warn(
92+
# GH#56815
93+
"make_block is deprecated and will be removed in a future version. "
94+
"Use pd.api.internals.create_dataframe_from_blocks or "
95+
"(recommended) higher-level public APIs instead.",
96+
DeprecationWarning,
97+
stacklevel=2,
98+
)
99+
90100
if dtype is not None:
91101
dtype = pandas_dtype(dtype)
92102

pandas/io/feather_format.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
TYPE_CHECKING,
77
Any,
88
)
9+
import warnings
910

1011
from pandas._config import using_pyarrow_string_dtype
1112

@@ -131,9 +132,16 @@ def read_feather(
131132
path, "rb", storage_options=storage_options, is_text=False
132133
) as handles:
133134
if dtype_backend is lib.no_default and not using_pyarrow_string_dtype():
134-
return feather.read_feather(
135-
handles.handle, columns=columns, use_threads=bool(use_threads)
136-
)
135+
with warnings.catch_warnings():
136+
warnings.filterwarnings(
137+
"ignore",
138+
"make_block is deprecated",
139+
DeprecationWarning,
140+
)
141+
142+
return feather.read_feather(
143+
handles.handle, columns=columns, use_threads=bool(use_threads)
144+
)
137145

138146
pa_table = feather.read_table(
139147
handles.handle, columns=columns, use_threads=bool(use_threads)

pandas/io/parquet.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
Any,
1111
Literal,
1212
)
13-
from warnings import catch_warnings
13+
from warnings import (
14+
catch_warnings,
15+
filterwarnings,
16+
)
1417

1518
from pandas._config import using_pyarrow_string_dtype
1619

@@ -271,7 +274,13 @@ def read(
271274
filters=filters,
272275
**kwargs,
273276
)
274-
result = pa_table.to_pandas(**to_pandas_kwargs)
277+
with catch_warnings():
278+
filterwarnings(
279+
"ignore",
280+
"make_block is deprecated",
281+
DeprecationWarning,
282+
)
283+
result = pa_table.to_pandas(**to_pandas_kwargs)
275284

276285
if pa_table.schema.metadata:
277286
if b"PANDAS_ATTRS" in pa_table.schema.metadata:
@@ -384,7 +393,15 @@ def read(
384393

385394
try:
386395
parquet_file = self.api.ParquetFile(path, **parquet_kwargs)
387-
return parquet_file.to_pandas(columns=columns, filters=filters, **kwargs)
396+
with catch_warnings():
397+
filterwarnings(
398+
"ignore",
399+
"make_block is deprecated",
400+
DeprecationWarning,
401+
)
402+
return parquet_file.to_pandas(
403+
columns=columns, filters=filters, **kwargs
404+
)
388405
finally:
389406
if handles is not None:
390407
handles.close()

pandas/io/parsers/arrow_parser_wrapper.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,23 @@ def read(self) -> DataFrame:
287287

288288
table = table.cast(new_schema)
289289

290-
if dtype_backend == "pyarrow":
291-
frame = table.to_pandas(types_mapper=pd.ArrowDtype)
292-
elif dtype_backend == "numpy_nullable":
293-
# Modify the default mapping to also
294-
# map null to Int64 (to match other engines)
295-
dtype_mapping = _arrow_dtype_mapping()
296-
dtype_mapping[pa.null()] = pd.Int64Dtype()
297-
frame = table.to_pandas(types_mapper=dtype_mapping.get)
298-
elif using_pyarrow_string_dtype():
299-
frame = table.to_pandas(types_mapper=arrow_string_types_mapper())
290+
with warnings.catch_warnings():
291+
warnings.filterwarnings(
292+
"ignore",
293+
"make_block is deprecated",
294+
DeprecationWarning,
295+
)
296+
if dtype_backend == "pyarrow":
297+
frame = table.to_pandas(types_mapper=pd.ArrowDtype)
298+
elif dtype_backend == "numpy_nullable":
299+
# Modify the default mapping to also
300+
# map null to Int64 (to match other engines)
301+
dtype_mapping = _arrow_dtype_mapping()
302+
dtype_mapping[pa.null()] = pd.Int64Dtype()
303+
frame = table.to_pandas(types_mapper=dtype_mapping.get)
304+
elif using_pyarrow_string_dtype():
305+
frame = table.to_pandas(types_mapper=arrow_string_types_mapper())
300306

301-
else:
302-
frame = table.to_pandas()
307+
else:
308+
frame = table.to_pandas()
303309
return self._finalize_pandas_output(frame)

pandas/tests/internals/test_api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ def test_namespace():
4444
def test_make_block_2d_with_dti():
4545
# GH#41168
4646
dti = pd.date_range("2012", periods=3, tz="UTC")
47-
blk = api.make_block(dti, placement=[0])
47+
48+
msg = "make_block is deprecated"
49+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
50+
blk = api.make_block(dti, placement=[0])
4851

4952
assert blk.shape == (1, 3)
5053
assert blk.values.shape == (1, 3)

pandas/tests/internals/test_internals.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -1368,8 +1368,10 @@ def test_validate_ndim():
13681368
placement = BlockPlacement(slice(2))
13691369
msg = r"Wrong number of dimensions. values.ndim != ndim \[1 != 2\]"
13701370

1371+
depr_msg = "make_block is deprecated"
13711372
with pytest.raises(ValueError, match=msg):
1372-
make_block(values, placement, ndim=2)
1373+
with tm.assert_produces_warning(DeprecationWarning, match=depr_msg):
1374+
make_block(values, placement, ndim=2)
13731375

13741376

13751377
def test_block_shape():
@@ -1384,23 +1386,29 @@ def test_make_block_no_pandas_array(block_maker):
13841386
# https://github.com/pandas-dev/pandas/pull/24866
13851387
arr = pd.arrays.NumpyExtensionArray(np.array([1, 2]))
13861388

1389+
depr_msg = "make_block is deprecated"
1390+
warn = DeprecationWarning if block_maker is make_block else None
1391+
13871392
# NumpyExtensionArray, no dtype
1388-
result = block_maker(arr, BlockPlacement(slice(len(arr))), ndim=arr.ndim)
1393+
with tm.assert_produces_warning(warn, match=depr_msg):
1394+
result = block_maker(arr, BlockPlacement(slice(len(arr))), ndim=arr.ndim)
13891395
assert result.dtype.kind in ["i", "u"]
13901396

13911397
if block_maker is make_block:
13921398
# new_block requires caller to unwrap NumpyExtensionArray
13931399
assert result.is_extension is False
13941400

13951401
# NumpyExtensionArray, NumpyEADtype
1396-
result = block_maker(arr, slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim)
1402+
with tm.assert_produces_warning(warn, match=depr_msg):
1403+
result = block_maker(arr, slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim)
13971404
assert result.dtype.kind in ["i", "u"]
13981405
assert result.is_extension is False
13991406

14001407
# new_block no longer accepts dtype keyword
14011408
# ndarray, NumpyEADtype
1402-
result = block_maker(
1403-
arr.to_numpy(), slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim
1404-
)
1409+
with tm.assert_produces_warning(warn, match=depr_msg):
1410+
result = block_maker(
1411+
arr.to_numpy(), slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim
1412+
)
14051413
assert result.dtype.kind in ["i", "u"]
14061414
assert result.is_extension is False

pandas/tests/io/test_parquet.py

+1
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ def test_filter_row_groups(self, pa):
985985
result = read_parquet(path, pa, filters=[("a", "==", 0)])
986986
assert len(result) == 1
987987

988+
@pytest.mark.filterwarnings("ignore:make_block is deprecated:DeprecationWarning")
988989
def test_read_dtype_backend_pyarrow_config(self, pa, df_full):
989990
import pyarrow
990991

0 commit comments

Comments
 (0)