Skip to content

Commit

Permalink
DEPR: deprecate .as_blocks()
Browse files Browse the repository at this point in the history
closes #17302
  • Loading branch information
jreback committed Sep 24, 2017
1 parent 965c1c8 commit ba88d50
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
21 changes: 15 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = {}
Expand All @@ -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',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down
12 changes: 10 additions & 2 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ba88d50

Please sign in to comment.