Skip to content

Commit ba88d50

Browse files
committed
DEPR: deprecate .as_blocks()
closes #17302
1 parent 965c1c8 commit ba88d50

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,9 @@ Other API Changes
488488
Deprecations
489489
~~~~~~~~~~~~
490490
- :func:`read_excel()` has deprecated ``sheetname`` in favor of ``sheet_name`` for consistency with ``.to_excel()`` (:issue:`10559`).
491-
492491
- ``pd.options.html.border`` has been deprecated in favor of ``pd.options.display.html.border`` (:issue:`15793`).
493-
494492
- :func:`SeriesGroupBy.nth` has deprecated ``True`` in favor of ``'all'`` for its kwarg ``dropna`` (:issue:`11038`).
493+
- :func:`DataFrame.as_blocks` is deprecated as this is exposing the internal implementation (:issue:`17302`)
495494

496495
.. _whatsnew_0210.prior_deprecations:
497496

pandas/core/generic.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -3692,6 +3692,8 @@ def as_blocks(self, copy=True):
36923692
Convert the frame to a dict of dtype -> Constructor Types that each has
36933693
a homogeneous dtype.
36943694
3695+
.. deprecated:: 0.21.0
3696+
36953697
NOTE: the dtypes of the blocks WILL BE PRESERVED HERE (unlike in
36963698
as_matrix)
36973699
@@ -3705,6 +3707,10 @@ def as_blocks(self, copy=True):
37053707
-------
37063708
values : a dict of dtype -> Constructor Types
37073709
"""
3710+
warnings.warn("as_blocks is deprecated and will "
3711+
"be removed in a future version",
3712+
FutureWarning, stacklevel=2)
3713+
37083714
self._consolidate_inplace()
37093715

37103716
bd = {}
@@ -3722,7 +3728,11 @@ def as_blocks(self, copy=True):
37223728

37233729
@property
37243730
def blocks(self):
3725-
"""Internal property, property synonym for as_blocks()"""
3731+
"""
3732+
Internal property, property synonym for as_blocks()
3733+
3734+
.. deprecated:: 0.21.0
3735+
"""
37263736
return self.as_blocks()
37273737

37283738
@deprecate_kwarg(old_arg_name='raise_on_error', new_arg_name='errors',
@@ -3931,13 +3941,12 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
39313941
-------
39323942
converted : same as input object
39333943
"""
3934-
from warnings import warn
39353944
msg = ("convert_objects is deprecated. To re-infer data dtypes for "
39363945
"object columns, use {klass}.infer_objects()\nFor all "
39373946
"other conversions use the data-type specific converters "
39383947
"pd.to_datetime, pd.to_timedelta and pd.to_numeric."
39393948
).format(klass=self.__class__.__name__)
3940-
warn(msg, FutureWarning, stacklevel=2)
3949+
warnings.warn(msg, FutureWarning, stacklevel=2)
39413950

39423951
return self._constructor(
39433952
self._data.convert(convert_dates=convert_dates,
@@ -4310,9 +4319,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
43104319
raise AssertionError("'to_replace' must be 'None' if 'regex' is "
43114320
"not a bool")
43124321
if axis is not None:
4313-
from warnings import warn
4314-
warn('the "axis" argument is deprecated and will be removed in'
4315-
'v0.13; this argument has no effect')
4322+
warnings.warn('the "axis" argument is deprecated '
4323+
'and will be removed in'
4324+
'v0.13; this argument has no effect')
43164325

43174326
self._consolidate_inplace()
43184327

pandas/tests/frame/test_block_internals.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ def test_copy_blocks(self):
320320
column = df.columns[0]
321321

322322
# use the default copy=True, change a column
323-
blocks = df.as_blocks()
323+
324+
# deprecated 0.21.0
325+
with tm.assert_produces_warning(FutureWarning,
326+
check_stacklevel=False):
327+
blocks = df.as_blocks()
324328
for dtype, _df in blocks.items():
325329
if column in _df:
326330
_df.loc[:, column] = _df[column] + 1
@@ -334,7 +338,11 @@ def test_no_copy_blocks(self):
334338
column = df.columns[0]
335339

336340
# use the copy=False, change a column
337-
blocks = df.as_blocks(copy=False)
341+
342+
# deprecated 0.21.0
343+
with tm.assert_produces_warning(FutureWarning,
344+
check_stacklevel=False):
345+
blocks = df.as_blocks(copy=False)
338346
for dtype, _df in blocks.items():
339347
if column in _df:
340348
_df.loc[:, column] = _df[column] + 1

pandas/tests/sparse/test_frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,10 @@ def test_as_blocks(self):
10991099
df = SparseDataFrame({'A': [1.1, 3.3], 'B': [nan, -3.9]},
11001100
dtype='float64')
11011101

1102-
df_blocks = df.blocks
1102+
# deprecated 0.21.0
1103+
with tm.assert_produces_warning(FutureWarning,
1104+
check_stacklevel=False):
1105+
df_blocks = df.blocks
11031106
assert list(df_blocks.keys()) == ['float64']
11041107
tm.assert_frame_equal(df_blocks['float64'], df)
11051108

0 commit comments

Comments
 (0)