From ba88d500beb724dd144f7f1900638ccf2855c61f Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Sun, 24 Sep 2017 10:07:53 -0400 Subject: [PATCH] DEPR: deprecate .as_blocks() closes #17302 --- doc/source/whatsnew/v0.21.0.txt | 3 +-- pandas/core/generic.py | 21 +++++++++++++++------ pandas/tests/frame/test_block_internals.py | 12 ++++++++++-- pandas/tests/sparse/test_frame.py | 5 ++++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 1365901c2ce5e3..ef312611d323ed 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -488,10 +488,9 @@ Other API Changes Deprecations ~~~~~~~~~~~~ - :func:`read_excel()` has deprecated ``sheetname`` in favor of ``sheet_name`` for consistency with ``.to_excel()`` (:issue:`10559`). - - ``pd.options.html.border`` has been deprecated in favor of ``pd.options.display.html.border`` (:issue:`15793`). - - :func:`SeriesGroupBy.nth` has deprecated ``True`` in favor of ``'all'`` for its kwarg ``dropna`` (:issue:`11038`). +- :func:`DataFrame.as_blocks` is deprecated as this is exposing the internal implementation (:issue:`17302`) .. _whatsnew_0210.prior_deprecations: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3d55e07df6eacb..a2b8ddb34d0d16 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3692,6 +3692,8 @@ def as_blocks(self, copy=True): Convert the frame to a dict of dtype -> Constructor Types that each has a homogeneous dtype. + .. deprecated:: 0.21.0 + NOTE: the dtypes of the blocks WILL BE PRESERVED HERE (unlike in as_matrix) @@ -3705,6 +3707,10 @@ def as_blocks(self, copy=True): ------- values : a dict of dtype -> Constructor Types """ + warnings.warn("as_blocks is deprecated and will " + "be removed in a future version", + FutureWarning, stacklevel=2) + self._consolidate_inplace() bd = {} @@ -3722,7 +3728,11 @@ def as_blocks(self, copy=True): @property def blocks(self): - """Internal property, property synonym for as_blocks()""" + """ + Internal property, property synonym for as_blocks() + + .. deprecated:: 0.21.0 + """ return self.as_blocks() @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, ------- converted : same as input object """ - from warnings import warn msg = ("convert_objects is deprecated. To re-infer data dtypes for " "object columns, use {klass}.infer_objects()\nFor all " "other conversions use the data-type specific converters " "pd.to_datetime, pd.to_timedelta and pd.to_numeric." ).format(klass=self.__class__.__name__) - warn(msg, FutureWarning, stacklevel=2) + warnings.warn(msg, FutureWarning, stacklevel=2) return self._constructor( self._data.convert(convert_dates=convert_dates, @@ -4310,9 +4319,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None, raise AssertionError("'to_replace' must be 'None' if 'regex' is " "not a bool") if axis is not None: - from warnings import warn - warn('the "axis" argument is deprecated and will be removed in' - 'v0.13; this argument has no effect') + warnings.warn('the "axis" argument is deprecated ' + 'and will be removed in' + 'v0.13; this argument has no effect') self._consolidate_inplace() diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index afa3c4f25789ae..3ca185cf158a7c 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -320,7 +320,11 @@ def test_copy_blocks(self): column = df.columns[0] # use the default copy=True, change a column - blocks = df.as_blocks() + + # deprecated 0.21.0 + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=False): + blocks = df.as_blocks() for dtype, _df in blocks.items(): if column in _df: _df.loc[:, column] = _df[column] + 1 @@ -334,7 +338,11 @@ def test_no_copy_blocks(self): column = df.columns[0] # use the copy=False, change a column - blocks = df.as_blocks(copy=False) + + # deprecated 0.21.0 + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=False): + blocks = df.as_blocks(copy=False) for dtype, _df in blocks.items(): if column in _df: _df.loc[:, column] = _df[column] + 1 diff --git a/pandas/tests/sparse/test_frame.py b/pandas/tests/sparse/test_frame.py index 004af5066fe835..ed4a3a9e5f75f8 100644 --- a/pandas/tests/sparse/test_frame.py +++ b/pandas/tests/sparse/test_frame.py @@ -1099,7 +1099,10 @@ def test_as_blocks(self): df = SparseDataFrame({'A': [1.1, 3.3], 'B': [nan, -3.9]}, dtype='float64') - df_blocks = df.blocks + # deprecated 0.21.0 + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=False): + df_blocks = df.blocks assert list(df_blocks.keys()) == ['float64'] tm.assert_frame_equal(df_blocks['float64'], df)