Skip to content

Commit dd5f0da

Browse files
jbrockmendelphofl
authored andcommitted
DEPR: deprecate exposing blocks in core.internals (pandas-dev#55139)
1 parent 3af631a commit dd5f0da

File tree

4 files changed

+91
-26
lines changed

4 files changed

+91
-26
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Other Deprecations
270270
- 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`)
271271
- 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`)
272272
- Deprecated :meth:`Index.format`, use ``index.astype(str)`` or ``index.map(formatter)`` instead (:issue:`55413`)
273+
- Deprecated ``core.internals`` members ``Block``, ``ExtensionBlock``, and ``DatetimeTZBlock``, use public APIs instead (:issue:`55139`)
273274
- Deprecated ``year``, ``month``, ``quarter``, ``day``, ``hour``, ``minute``, and ``second`` keywords in the :class:`PeriodIndex` constructor, use :meth:`PeriodIndex.from_fields` instead (:issue:`55960`)
274275
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_clipboard`. (:issue:`54229`)
275276
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_csv` except ``path_or_buf``. (:issue:`54229`)

pandas/core/internals/__init__.py

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pandas.core.internals.api import make_block
1+
from pandas.core.internals.api import make_block # 2023-09-18 pyarrow uses this
22
from pandas.core.internals.array_manager import (
33
ArrayManager,
44
SingleArrayManager,
@@ -7,21 +7,16 @@
77
DataManager,
88
SingleDataManager,
99
)
10-
from pandas.core.internals.blocks import ( # io.pytables, io.packers
11-
Block,
12-
DatetimeTZBlock,
13-
ExtensionBlock,
14-
)
1510
from pandas.core.internals.concat import concatenate_managers
1611
from pandas.core.internals.managers import (
1712
BlockManager,
1813
SingleBlockManager,
1914
)
2015

2116
__all__ = [
22-
"Block",
23-
"DatetimeTZBlock",
24-
"ExtensionBlock",
17+
"Block", # pylint: disable=undefined-all-variable
18+
"DatetimeTZBlock", # pylint: disable=undefined-all-variable
19+
"ExtensionBlock", # pylint: disable=undefined-all-variable
2520
"make_block",
2621
"DataManager",
2722
"ArrayManager",
@@ -34,33 +29,54 @@
3429

3530

3631
def __getattr__(name: str):
32+
# GH#55139
3733
import warnings
3834

39-
from pandas.util._exceptions import find_stack_level
40-
4135
if name == "create_block_manager_from_blocks":
4236
# GH#33892
4337
warnings.warn(
4438
f"{name} is deprecated and will be removed in a future version. "
4539
"Use public APIs instead.",
4640
DeprecationWarning,
47-
stacklevel=find_stack_level(),
41+
# https://github.com/pandas-dev/pandas/pull/55139#pullrequestreview-1720690758
42+
# on hard-coding stacklevel
43+
stacklevel=2,
4844
)
4945
from pandas.core.internals.managers import create_block_manager_from_blocks
5046

5147
return create_block_manager_from_blocks
5248

53-
if name in ["NumericBlock", "ObjectBlock"]:
49+
if name in [
50+
"NumericBlock",
51+
"ObjectBlock",
52+
"Block",
53+
"ExtensionBlock",
54+
"DatetimeTZBlock",
55+
]:
5456
warnings.warn(
5557
f"{name} is deprecated and will be removed in a future version. "
5658
"Use public APIs instead.",
5759
DeprecationWarning,
58-
stacklevel=find_stack_level(),
60+
# https://github.com/pandas-dev/pandas/pull/55139#pullrequestreview-1720690758
61+
# on hard-coding stacklevel
62+
stacklevel=2,
5963
)
6064
if name == "NumericBlock":
6165
from pandas.core.internals.blocks import NumericBlock
6266

6367
return NumericBlock
68+
elif name == "DatetimeTZBlock":
69+
from pandas.core.internals.blocks import DatetimeTZBlock
70+
71+
return DatetimeTZBlock
72+
elif name == "ExtensionBlock":
73+
from pandas.core.internals.blocks import ExtensionBlock
74+
75+
return ExtensionBlock
76+
elif name == "Block":
77+
from pandas.core.internals.blocks import Block
78+
79+
return Block
6480
else:
6581
from pandas.core.internals.blocks import ObjectBlock
6682

pandas/core/internals/api.py

+36-9
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
from pandas.core.arrays import DatetimeArray
2424
from pandas.core.construction import extract_array
2525
from pandas.core.internals.blocks import (
26-
Block,
27-
DatetimeTZBlock,
28-
ExtensionBlock,
2926
check_ndim,
3027
ensure_block_shape,
3128
extract_pandas_array,
@@ -36,6 +33,8 @@
3633
if TYPE_CHECKING:
3734
from pandas._typing import Dtype
3835

36+
from pandas.core.internals.blocks import Block
37+
3938

4039
def make_block(
4140
values, placement, klass=None, ndim=None, dtype: Dtype | None = None
@@ -56,6 +55,11 @@ def make_block(
5655

5756
values, dtype = extract_pandas_array(values, dtype, ndim)
5857

58+
from pandas.core.internals.blocks import (
59+
DatetimeTZBlock,
60+
ExtensionBlock,
61+
)
62+
5963
if klass is ExtensionBlock and isinstance(values.dtype, PeriodDtype):
6064
# GH-44681 changed PeriodArray to be stored in the 2D
6165
# NDArrayBackedExtensionBlock instead of ExtensionBlock
@@ -108,21 +112,44 @@ def maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int
108112

109113

110114
def __getattr__(name: str):
115+
# GH#55139
111116
import warnings
112117

113-
from pandas.util._exceptions import find_stack_level
114-
115-
if name == "create_block_manager_from_blocks":
118+
if name in [
119+
"Block",
120+
"ExtensionBlock",
121+
"DatetimeTZBlock",
122+
"create_block_manager_from_blocks",
123+
]:
116124
# GH#33892
117125
warnings.warn(
118126
f"{name} is deprecated and will be removed in a future version. "
119127
"Use public APIs instead.",
120128
DeprecationWarning,
121-
stacklevel=find_stack_level(),
129+
# https://github.com/pandas-dev/pandas/pull/55139#pullrequestreview-1720690758
130+
# on hard-coding stacklevel
131+
stacklevel=2,
122132
)
123-
from pandas.core.internals.managers import create_block_manager_from_blocks
124133

125-
return create_block_manager_from_blocks
134+
if name == "create_block_manager_from_blocks":
135+
from pandas.core.internals.managers import create_block_manager_from_blocks
136+
137+
return create_block_manager_from_blocks
138+
139+
elif name == "Block":
140+
from pandas.core.internals.blocks import Block
141+
142+
return Block
143+
144+
elif name == "DatetimeTZBlock":
145+
from pandas.core.internals.blocks import DatetimeTZBlock
146+
147+
return DatetimeTZBlock
148+
149+
elif name == "ExtensionBlock":
150+
from pandas.core.internals.blocks import ExtensionBlock
151+
152+
return ExtensionBlock
126153

127154
raise AttributeError(
128155
f"module 'pandas.core.internals.api' has no attribute '{name}'"

pandas/tests/internals/test_api.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
in core.internals
44
"""
55

6+
import pytest
7+
68
import pandas as pd
79
import pandas._testing as tm
810
from pandas.core import internals
@@ -27,9 +29,6 @@ def test_namespace():
2729
"ops",
2830
]
2931
expected = [
30-
"Block",
31-
"DatetimeTZBlock",
32-
"ExtensionBlock",
3332
"make_block",
3433
"DataManager",
3534
"ArrayManager",
@@ -44,6 +43,28 @@ def test_namespace():
4443
assert set(result) == set(expected + modules)
4544

4645

46+
@pytest.mark.parametrize(
47+
"name",
48+
[
49+
"NumericBlock",
50+
"ObjectBlock",
51+
"Block",
52+
"ExtensionBlock",
53+
"DatetimeTZBlock",
54+
],
55+
)
56+
def test_deprecations(name):
57+
# GH#55139
58+
msg = f"{name} is deprecated.* Use public APIs instead"
59+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
60+
getattr(internals, name)
61+
62+
if name not in ["NumericBlock", "ObjectBlock"]:
63+
# NumericBlock and ObjectBlock are not in the internals.api namespace
64+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
65+
getattr(api, name)
66+
67+
4768
def test_make_block_2d_with_dti():
4869
# GH#41168
4970
dti = pd.date_range("2012", periods=3, tz="UTC")

0 commit comments

Comments
 (0)