Skip to content

DEPR: make_block #57754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ starting with 3.0, so it can be safely removed from your code.
Other Deprecations
^^^^^^^^^^^^^^^^^^

- Deprecated :func:`core.internals.api.make_block`, use public APIs instead (:issue:`56815`)
- Deprecated :meth:`.DataFrameGroupby.corrwith` (:issue:`57158`)
- Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`)
- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`)
Expand All @@ -272,7 +273,6 @@ Other Deprecations
- 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`)
- Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`)
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_300.prior_deprecations:
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/internals/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING
import warnings

import numpy as np

Expand Down Expand Up @@ -87,6 +88,15 @@ def make_block(
- Block.make_block_same_class
- Block.__init__
"""
warnings.warn(
# GH#56815
"make_block is deprecated and will be removed in a future version. "
"Use pd.api.internals.create_dataframe_from_blocks or "
"(recommended) higher-level public APIs instead.",
DeprecationWarning,
stacklevel=2,
)

if dtype is not None:
dtype = pandas_dtype(dtype)

Expand Down
14 changes: 11 additions & 3 deletions pandas/io/feather_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
TYPE_CHECKING,
Any,
)
import warnings

from pandas._config import using_pyarrow_string_dtype

Expand Down Expand Up @@ -131,9 +132,16 @@ def read_feather(
path, "rb", storage_options=storage_options, is_text=False
) as handles:
if dtype_backend is lib.no_default and not using_pyarrow_string_dtype():
return feather.read_feather(
handles.handle, columns=columns, use_threads=bool(use_threads)
)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
"make_block is deprecated",
DeprecationWarning,
)

return feather.read_feather(
handles.handle, columns=columns, use_threads=bool(use_threads)
)
Comment on lines +135 to +144
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this? (the fix is in a released pyarrow, so with latest of both you won't see any warning. Although of course when testing with an older pyarrow you will see it, but I am not sure we added such warning suppressions for other deprecations like DataFrame(mgr)?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need it, no. we need to catch this either here or filterwarnings in a bunch of test places. so i prefer to catch it one place, but OK either way


pa_table = feather.read_table(
handles.handle, columns=columns, use_threads=bool(use_threads)
Expand Down
23 changes: 20 additions & 3 deletions pandas/io/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
Any,
Literal,
)
from warnings import catch_warnings
from warnings import (
catch_warnings,
filterwarnings,
)

from pandas._config import using_pyarrow_string_dtype

Expand Down Expand Up @@ -271,7 +274,13 @@ def read(
filters=filters,
**kwargs,
)
result = pa_table.to_pandas(**to_pandas_kwargs)
with catch_warnings():
filterwarnings(
"ignore",
"make_block is deprecated",
DeprecationWarning,
)
result = pa_table.to_pandas(**to_pandas_kwargs)

if pa_table.schema.metadata:
if b"PANDAS_ATTRS" in pa_table.schema.metadata:
Expand Down Expand Up @@ -384,7 +393,15 @@ def read(

try:
parquet_file = self.api.ParquetFile(path, **parquet_kwargs)
return parquet_file.to_pandas(columns=columns, filters=filters, **kwargs)
with catch_warnings():
filterwarnings(
"ignore",
"make_block is deprecated",
DeprecationWarning,
)
return parquet_file.to_pandas(
columns=columns, filters=filters, **kwargs
)
finally:
if handles is not None:
handles.close()
Expand Down
30 changes: 18 additions & 12 deletions pandas/io/parsers/arrow_parser_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,23 @@ def read(self) -> DataFrame:

table = table.cast(new_schema)

if dtype_backend == "pyarrow":
frame = table.to_pandas(types_mapper=pd.ArrowDtype)
elif dtype_backend == "numpy_nullable":
# Modify the default mapping to also
# map null to Int64 (to match other engines)
dtype_mapping = _arrow_dtype_mapping()
dtype_mapping[pa.null()] = pd.Int64Dtype()
frame = table.to_pandas(types_mapper=dtype_mapping.get)
elif using_pyarrow_string_dtype():
frame = table.to_pandas(types_mapper=arrow_string_types_mapper())
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
"make_block is deprecated",
DeprecationWarning,
)
if dtype_backend == "pyarrow":
frame = table.to_pandas(types_mapper=pd.ArrowDtype)
elif dtype_backend == "numpy_nullable":
# Modify the default mapping to also
# map null to Int64 (to match other engines)
dtype_mapping = _arrow_dtype_mapping()
dtype_mapping[pa.null()] = pd.Int64Dtype()
frame = table.to_pandas(types_mapper=dtype_mapping.get)
elif using_pyarrow_string_dtype():
frame = table.to_pandas(types_mapper=arrow_string_types_mapper())

else:
frame = table.to_pandas()
else:
frame = table.to_pandas()
return self._finalize_pandas_output(frame)
5 changes: 4 additions & 1 deletion pandas/tests/internals/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def test_namespace():
def test_make_block_2d_with_dti():
# GH#41168
dti = pd.date_range("2012", periods=3, tz="UTC")
blk = api.make_block(dti, placement=[0])

msg = "make_block is deprecated"
with tm.assert_produces_warning(DeprecationWarning, match=msg):
blk = api.make_block(dti, placement=[0])

assert blk.shape == (1, 3)
assert blk.values.shape == (1, 3)
Expand Down
20 changes: 14 additions & 6 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,8 +1368,10 @@ def test_validate_ndim():
placement = BlockPlacement(slice(2))
msg = r"Wrong number of dimensions. values.ndim != ndim \[1 != 2\]"

depr_msg = "make_block is deprecated"
with pytest.raises(ValueError, match=msg):
make_block(values, placement, ndim=2)
with tm.assert_produces_warning(DeprecationWarning, match=depr_msg):
make_block(values, placement, ndim=2)


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

depr_msg = "make_block is deprecated"
warn = DeprecationWarning if block_maker is make_block else None

# NumpyExtensionArray, no dtype
result = block_maker(arr, BlockPlacement(slice(len(arr))), ndim=arr.ndim)
with tm.assert_produces_warning(warn, match=depr_msg):
result = block_maker(arr, BlockPlacement(slice(len(arr))), ndim=arr.ndim)
assert result.dtype.kind in ["i", "u"]

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

# NumpyExtensionArray, NumpyEADtype
result = block_maker(arr, slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim)
with tm.assert_produces_warning(warn, match=depr_msg):
result = block_maker(arr, slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim)
assert result.dtype.kind in ["i", "u"]
assert result.is_extension is False

# new_block no longer accepts dtype keyword
# ndarray, NumpyEADtype
result = block_maker(
arr.to_numpy(), slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim
)
with tm.assert_produces_warning(warn, match=depr_msg):
result = block_maker(
arr.to_numpy(), slice(len(arr)), dtype=arr.dtype, ndim=arr.ndim
)
assert result.dtype.kind in ["i", "u"]
assert result.is_extension is False
1 change: 1 addition & 0 deletions pandas/tests/io/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ def test_filter_row_groups(self, pa):
result = read_parquet(path, pa, filters=[("a", "==", 0)])
assert len(result) == 1

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

Expand Down
Loading