diff --git a/doc/source/10min.rst b/doc/source/10min.rst index ef6b2d6ef2c90..ede52908bbe0f 100644 --- a/doc/source/10min.rst +++ b/doc/source/10min.rst @@ -95,17 +95,7 @@ will be completed: df2.append df2.combine_first df2.apply df2.compound df2.applymap df2.consolidate - df2.as_blocks df2.convert_objects - df2.asfreq df2.copy - df2.as_matrix df2.corr - df2.astype df2.corrwith - df2.at df2.count - df2.at_time df2.cov - df2.axes df2.cummax - df2.B df2.cummin - df2.between_time df2.cumprod - df2.bfill df2.cumsum - df2.blocks df2.D + df2.D As you can see, the columns ``A``, ``B``, ``C``, and ``D`` are automatically tab completed. ``E`` is there as well; the rest of the attributes have been diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 1365901c2ce5e..07cc00b3724e4 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/computation/expressions.py b/pandas/core/computation/expressions.py index af068bd1f32b3..8ddc625887a51 100644 --- a/pandas/core/computation/expressions.py +++ b/pandas/core/computation/expressions.py @@ -165,7 +165,7 @@ def _has_bool_dtype(x): return x.dtype == bool except AttributeError: try: - return 'bool' in x.blocks + return 'bool' in x.dtypes except AttributeError: return isinstance(x, (bool, np.bool_)) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3d55e07df6eac..a6147aa4fd8ee 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) @@ -3699,7 +3701,25 @@ def as_blocks(self, copy=True): ---------- copy : boolean, default True - .. versionadded: 0.16.1 + Returns + ------- + values : a dict of dtype -> Constructor Types + """ + warnings.warn("as_blocks is deprecated and will " + "be removed in a future version", + FutureWarning, stacklevel=2) + return self._as_blocks(copy=copy) + + def _as_blocks(self, copy=True): + """ + Convert the frame to a dict of dtype -> Constructor Types that each has + a homogeneous dtype. + + Internal routine only + + Parameters + ---------- + copy : boolean, default True Returns ------- @@ -3722,7 +3742,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 +3955,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 +4333,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/core/window.py b/pandas/core/window.py index 4bd959f52673c..7645b3d9611c3 100644 --- a/pandas/core/window.py +++ b/pandas/core/window.py @@ -141,7 +141,7 @@ def _create_blocks(self, how): if obj.ndim == 2: obj = obj.reindex(columns=obj.columns.difference([self.on]), copy=False) - blocks = obj.as_blocks(copy=False).values() + blocks = obj._as_blocks(copy=False).values() return blocks, obj, index diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index afa3c4f25789a..3ca185cf158a7 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/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index d942330ecd8a6..de11afe2cd19f 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1766,7 +1766,7 @@ def test_from_records_sequencelike(self): # this is actually tricky to create the recordlike arrays and # have the dtypes be intact - blocks = df.blocks + blocks = df._as_blocks() tuples = [] columns = [] dtypes = [] @@ -1842,7 +1842,7 @@ def test_from_records_dictlike(self): # columns is in a different order here than the actual items iterated # from the dict columns = [] - for dtype, b in compat.iteritems(df.blocks): + for dtype, b in compat.iteritems(df._as_blocks()): columns.extend(b.columns) asdict = dict((x, y) for x, y in compat.iteritems(df)) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 0900d21b250ed..e54dd9bb66831 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -469,10 +469,10 @@ def test_set_change_dtype_slice(self): # GH8850 df = DataFrame([[1.0, 2, 3], [4.0, 5, 6]], columns=cols) df['2nd'] = df['2nd'] * 2.0 - assert sorted(df.blocks.keys()) == ['float64', 'int64'] - assert_frame_equal(df.blocks['float64'], DataFrame( + assert sorted(df._as_blocks().keys()) == ['float64', 'int64'] + assert_frame_equal(df._as_blocks()['float64'], DataFrame( [[1.0, 4.0], [4.0, 10.0]], columns=cols[:2])) - assert_frame_equal(df.blocks['int64'], DataFrame( + assert_frame_equal(df._as_blocks()['int64'], DataFrame( [[3], [6]], columns=cols[2:])) def test_copy(self, mgr): diff --git a/pandas/tests/sparse/test_frame.py b/pandas/tests/sparse/test_frame.py index 004af5066fe83..ed4a3a9e5f75f 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)