From eed2fb6df64828143ce8794ce963fc1fdd91f437 Mon Sep 17 00:00:00 2001 From: tp Date: Sat, 25 Nov 2017 22:41:32 +0000 Subject: [PATCH] updating according to comments --- doc/source/whatsnew/v0.22.0.txt | 2 +- pandas/core/generic.py | 12 ++++---- pandas/core/internals.py | 11 +++++--- pandas/tests/frame/test_api.py | 9 +++++- pandas/tests/frame/test_block_internals.py | 32 +++++++++++----------- 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index c99d2f2df86c09..f1354161f16088 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -87,7 +87,7 @@ Deprecations ~~~~~~~~~~~~ - ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`). -- ``NDFrame.as_matrix`` is deprecated. Use ``NDFrame.values`` instead (:issue:`18458`). +- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`). - .. _whatsnew_0220.prior_deprecations: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1368bc00be465d..bc4783c82f83d7 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3735,8 +3735,8 @@ def _get_bool_data(self): def as_matrix(self, columns=None): """ - DEPRECATED: This method will be removed in a future version. - Use :meth:`NDFrame.values` instead. + DEPRECATED: as_matrix will be removed in a future version. + Use :meth:`DataFrame.values` instead. Convert the frame to its Numpy-array representation. @@ -3773,8 +3773,8 @@ def as_matrix(self, columns=None): -------- pandas.DataFrame.values """ - warnings.warn("This method will be removed in a future version. " - "Use ``.values`` instead.") + warnings.warn("method ``as_matrix`` will be removed in a future version. " + "Use ``values`` instead.", FutureWarning, stacklevel=2) self._consolidate_inplace() if self._AXIS_REVERSED: return self._data.as_matrix(columns).T @@ -3797,9 +3797,7 @@ def values(self): will result in a flot64 dtype. """ self._consolidate_inplace() - if self._AXIS_REVERSED: - return self._data.as_matrix().T - return self._data.as_matrix() + return self._data.as_matrix(transpose=self._AXIS_REVERSED) @property def _values(self): diff --git a/pandas/core/internals.py b/pandas/core/internals.py index e537cb2edc1c45..ba9cd64a8985e3 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -3670,9 +3670,10 @@ def copy(self, deep=True, mgr=None): return self.apply('copy', axes=new_axes, deep=deep, do_integrity_check=False) - def as_matrix(self, items=None): + def as_matrix(self, transpose=False, items=None): if len(self.blocks) == 0: - return np.empty(self.shape, dtype=float) + arr = np.empty(self.shape, dtype=float) + return arr.transpose() if transpose else arr if items is not None: mgr = self.reindex_axis(items, axis=0) @@ -3680,9 +3681,11 @@ def as_matrix(self, items=None): mgr = self if self._is_single_block or not self.is_mixed_type: - return mgr.blocks[0].get_values() + arr = mgr.blocks[0].get_values else: - return mgr._interleave() + arr = mgr._interleave() + + return arr.transpose() if transpose else arr def _interleave(self): """ diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 654f920d86c3af..6538a98e5fa7d0 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -257,7 +257,7 @@ def test_values(self): assert value == frame[col][i] # mixed type - mat = self.mixed_frame.as_matrix(['foo', 'A']) + mat = self.mixed_frame[['foo', 'A']].values assert mat[0, 0] == 'bar' df = self.klass({'real': [1, 2, 3], 'complex': [1j, 2j, 3j]}) @@ -369,6 +369,13 @@ def test_values(self): self.frame.values[:, 0] = 5. assert (self.frame.values[:, 0] == 5).all() + def test_as_matrix_deprecated(self): + # GH18458 + with tm.assert_produces_warning(FutureWarning): + result = self.frame.as_matrix() + expected = self.frame.values + tm.assert_numpy_array_equal(result, expected) + def test_deepcopy(self): cp = deepcopy(self.frame) series = cp['A'] diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index c29821ba51284f..8b1fd7d50cb4db 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -67,10 +67,10 @@ def test_consolidate_inplace(self): for letter in range(ord('A'), ord('Z')): self.frame[chr(letter)] = chr(letter) - def test_as_matrix_consolidate(self): + def test_values_consolidate(self): self.frame['E'] = 7. assert not self.frame._data.is_consolidated() - _ = self.frame.as_matrix() # noqa + _ = self.frame.values # noqa assert self.frame._data.is_consolidated() def test_modify_values(self): @@ -91,50 +91,50 @@ def test_boolean_set_uncons(self): self.frame[self.frame > 1] = 2 assert_almost_equal(expected, self.frame.values) - def test_as_matrix_numeric_cols(self): + def test_values_numeric_cols(self): self.frame['foo'] = 'bar' - values = self.frame.as_matrix(['A', 'B', 'C', 'D']) + values = self.frame[['A', 'B', 'C', 'D']].values assert values.dtype == np.float64 - def test_as_matrix_lcd(self): + def test_values_lcd(self): # mixed lcd - values = self.mixed_float.as_matrix(['A', 'B', 'C', 'D']) + values = self.mixed_float[['A', 'B', 'C', 'D']].values assert values.dtype == np.float64 - values = self.mixed_float.as_matrix(['A', 'B', 'C']) + values = self.mixed_float[['A', 'B', 'C']].values assert values.dtype == np.float32 - values = self.mixed_float.as_matrix(['C']) + values = self.mixed_float[['C']].values assert values.dtype == np.float16 # GH 10364 # B uint64 forces float because there are other signed int types - values = self.mixed_int.as_matrix(['A', 'B', 'C', 'D']) + values = self.mixed_int[['A', 'B', 'C', 'D']].values assert values.dtype == np.float64 - values = self.mixed_int.as_matrix(['A', 'D']) + values = self.mixed_int[['A', 'D']].values assert values.dtype == np.int64 # B uint64 forces float because there are other signed int types - values = self.mixed_int.as_matrix(['A', 'B', 'C']) + values = self.mixed_int[['A', 'B', 'C']].values assert values.dtype == np.float64 # as B and C are both unsigned, no forcing to float is needed - values = self.mixed_int.as_matrix(['B', 'C']) + values = self.mixed_int[['B', 'C']].values assert values.dtype == np.uint64 - values = self.mixed_int.as_matrix(['A', 'C']) + values = self.mixed_int[['A', 'C']].values assert values.dtype == np.int32 - values = self.mixed_int.as_matrix(['C', 'D']) + values = self.mixed_int[['C', 'D']].values assert values.dtype == np.int64 - values = self.mixed_int.as_matrix(['A']) + values = self.mixed_int[['A']].values assert values.dtype == np.int32 - values = self.mixed_int.as_matrix(['C']) + values = self.mixed_int[['C']].values assert values.dtype == np.uint8 def test_constructor_with_convert(self):