diff --git a/pandas/core/generic.py b/pandas/core/generic.py index bc4783c82f83d7..54b0089335b198 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3773,12 +3773,11 @@ def as_matrix(self, columns=None): -------- pandas.DataFrame.values """ - warnings.warn("method ``as_matrix`` will be removed in a future version. " - "Use ``values`` instead.", FutureWarning, stacklevel=2) + 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 - return self._data.as_matrix(columns) + return self._data.as_array(transpose=self._AXIS_REVERSED, + items=columns) @property def values(self): @@ -3797,7 +3796,7 @@ def values(self): will result in a flot64 dtype. """ self._consolidate_inplace() - return self._data.as_matrix(transpose=self._AXIS_REVERSED) + return self._data.as_array(transpose=self._AXIS_REVERSED) @property def _values(self): diff --git a/pandas/core/internals.py b/pandas/core/internals.py index bc1e1651f689ff..4f25a19d437cac 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -3484,7 +3484,7 @@ def replace_list(self, src_list, dest_list, inplace=False, regex=False, mgr = self # figure out our mask a-priori to avoid repeated replacements - values = self.as_matrix() + values = self.as_array() def comp(s): if isna(s): @@ -3670,7 +3670,21 @@ def copy(self, deep=True, mgr=None): return self.apply('copy', axes=new_axes, deep=deep, do_integrity_check=False) - def as_matrix(self, transpose=False, items=None): + def as_array(self, transpose=False, items=None): + """Convert the blockmanager data into an numpy array. + + Parameters + ---------- + transpose : boolean, default False + If True, transpose the return array + items : list of strings or None + Names of block items that will be included in the returned + array. ``None`` means that all block items will be used + + Returns + ------- + arr : ndarray + """ if len(self.blocks) == 0: arr = np.empty(self.shape, dtype=float) return arr.transpose() if transpose else arr diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 0a5e705071b5e9..0f3c5cb85249a9 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -464,7 +464,7 @@ def to_excel(self, path, na_rep='', engine=None, **kwargs): def as_matrix(self): self._consolidate_inplace() - return self._data.as_matrix() + return self._data.as_array() # ---------------------------------------------------------------------- # Getting and setting elements diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 6538a98e5fa7d0..c894e6a86487a8 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -372,7 +372,7 @@ def test_values(self): def test_as_matrix_deprecated(self): # GH18458 with tm.assert_produces_warning(FutureWarning): - result = self.frame.as_matrix() + result = self.frame.as_matrix(columns=self.frame.columns.tolist()) expected = self.frame.values tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 4c0c7d8598a8e6..a22d0174947e13 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -476,7 +476,7 @@ def test_copy(self, mgr): def test_sparse(self): mgr = create_mgr('a: sparse-1; b: sparse-2') # what to test here? - assert mgr.as_matrix().dtype == np.float64 + assert mgr.as_array().dtype == np.float64 def test_sparse_mixed(self): mgr = create_mgr('a: sparse-1; b: sparse-2; c: f8') @@ -485,32 +485,32 @@ def test_sparse_mixed(self): # what to test here? - def test_as_matrix_float(self): + def test_as_array_float(self): mgr = create_mgr('c: f4; d: f2; e: f8') - assert mgr.as_matrix().dtype == np.float64 + assert mgr.as_array().dtype == np.float64 mgr = create_mgr('c: f4; d: f2') - assert mgr.as_matrix().dtype == np.float32 + assert mgr.as_array().dtype == np.float32 - def test_as_matrix_int_bool(self): + def test_as_array_int_bool(self): mgr = create_mgr('a: bool-1; b: bool-2') - assert mgr.as_matrix().dtype == np.bool_ + assert mgr.as_array().dtype == np.bool_ mgr = create_mgr('a: i8-1; b: i8-2; c: i4; d: i2; e: u1') - assert mgr.as_matrix().dtype == np.int64 + assert mgr.as_array().dtype == np.int64 mgr = create_mgr('c: i4; d: i2; e: u1') - assert mgr.as_matrix().dtype == np.int32 + assert mgr.as_array().dtype == np.int32 - def test_as_matrix_datetime(self): + def test_as_array_datetime(self): mgr = create_mgr('h: datetime-1; g: datetime-2') - assert mgr.as_matrix().dtype == 'M8[ns]' + assert mgr.as_array().dtype == 'M8[ns]' - def test_as_matrix_datetime_tz(self): + def test_as_array_datetime_tz(self): mgr = create_mgr('h: M8[ns, US/Eastern]; g: M8[ns, CET]') assert mgr.get('h').dtype == 'datetime64[ns, US/Eastern]' assert mgr.get('g').dtype == 'datetime64[ns, CET]' - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' def test_astype(self): # coerce all @@ -607,49 +607,49 @@ def test_interleave(self): for dtype in ['f8', 'i8', 'object', 'bool', 'complex', 'M8[ns]', 'm8[ns]']: mgr = create_mgr('a: {0}'.format(dtype)) - assert mgr.as_matrix().dtype == dtype + assert mgr.as_array().dtype == dtype mgr = create_mgr('a: {0}; b: {0}'.format(dtype)) - assert mgr.as_matrix().dtype == dtype + assert mgr.as_array().dtype == dtype # will be converted according the actual dtype of the underlying mgr = create_mgr('a: category') - assert mgr.as_matrix().dtype == 'i8' + assert mgr.as_array().dtype == 'i8' mgr = create_mgr('a: category; b: category') - assert mgr.as_matrix().dtype == 'i8' + assert mgr.as_array().dtype == 'i8' mgr = create_mgr('a: category; b: category2') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: category2') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: category2; b: category2') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' # combinations mgr = create_mgr('a: f8') - assert mgr.as_matrix().dtype == 'f8' + assert mgr.as_array().dtype == 'f8' mgr = create_mgr('a: f8; b: i8') - assert mgr.as_matrix().dtype == 'f8' + assert mgr.as_array().dtype == 'f8' mgr = create_mgr('a: f4; b: i8') - assert mgr.as_matrix().dtype == 'f8' + assert mgr.as_array().dtype == 'f8' mgr = create_mgr('a: f4; b: i8; d: object') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: bool; b: i8') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: complex') - assert mgr.as_matrix().dtype == 'complex' + assert mgr.as_array().dtype == 'complex' mgr = create_mgr('a: f8; b: category') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: M8[ns]; b: category') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: M8[ns]; b: bool') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: M8[ns]; b: i8') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: m8[ns]; b: bool') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: m8[ns]; b: i8') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' mgr = create_mgr('a: M8[ns]; b: m8[ns]') - assert mgr.as_matrix().dtype == 'object' + assert mgr.as_array().dtype == 'object' def test_interleave_non_unique_cols(self): df = DataFrame([ @@ -831,7 +831,7 @@ def test_equals_block_order_different_dtypes(self): def test_single_mgr_ctor(self): mgr = create_single_mgr('f8', num_rows=5) - assert mgr.as_matrix().tolist() == [0., 1., 2., 3., 4.] + assert mgr.as_array().tolist() == [0., 1., 2., 3., 4.] def test_validate_bool_args(self): invalid_values = [1, "True", [1, 2, 3], 5.0] @@ -878,7 +878,7 @@ class TestIndexing(object): def test_get_slice(self): def assert_slice_ok(mgr, axis, slobj): # import pudb; pudb.set_trace() - mat = mgr.as_matrix() + mat = mgr.as_array() # we maybe using an ndarray to test slicing and # might not be the full length of the axis @@ -889,7 +889,7 @@ def assert_slice_ok(mgr, axis, slobj): len(ax) - len(slobj), dtype=bool)]) sliced = mgr.get_slice(slobj, axis=axis) mat_slobj = (slice(None), ) * axis + (slobj, ) - tm.assert_numpy_array_equal(mat[mat_slobj], sliced.as_matrix(), + tm.assert_numpy_array_equal(mat[mat_slobj], sliced.as_array(), check_dtype=False) tm.assert_index_equal(mgr.axes[axis][slobj], sliced.axes[axis]) @@ -930,10 +930,10 @@ def assert_slice_ok(mgr, axis, slobj): def test_take(self): def assert_take_ok(mgr, axis, indexer): - mat = mgr.as_matrix() + mat = mgr.as_array() taken = mgr.take(indexer, axis) tm.assert_numpy_array_equal(np.take(mat, indexer, axis), - taken.as_matrix(), check_dtype=False) + taken.as_array(), check_dtype=False) tm.assert_index_equal(mgr.axes[axis].take(indexer), taken.axes[axis]) @@ -950,14 +950,14 @@ def assert_take_ok(mgr, axis, indexer): def test_reindex_axis(self): def assert_reindex_axis_is_ok(mgr, axis, new_labels, fill_value): - mat = mgr.as_matrix() + mat = mgr.as_array() indexer = mgr.axes[axis].get_indexer_for(new_labels) reindexed = mgr.reindex_axis(new_labels, axis, fill_value=fill_value) tm.assert_numpy_array_equal(algos.take_nd(mat, indexer, axis, fill_value=fill_value), - reindexed.as_matrix(), + reindexed.as_array(), check_dtype=False) tm.assert_index_equal(reindexed.axes[axis], new_labels) @@ -996,13 +996,13 @@ def test_reindex_indexer(self): def assert_reindex_indexer_is_ok(mgr, axis, new_labels, indexer, fill_value): - mat = mgr.as_matrix() + mat = mgr.as_array() reindexed_mat = algos.take_nd(mat, indexer, axis, fill_value=fill_value) reindexed = mgr.reindex_indexer(new_labels, indexer, axis, fill_value=fill_value) tm.assert_numpy_array_equal(reindexed_mat, - reindexed.as_matrix(), + reindexed.as_array(), check_dtype=False) tm.assert_index_equal(reindexed.axes[axis], new_labels)