Skip to content

Commit 7fd4b71

Browse files
author
tp
committed
Deprecate NDFrame.as_matrix
1 parent b45325e commit 7fd4b71

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Deprecations
8282
~~~~~~~~~~~~
8383

8484
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
85-
-
85+
- ``NDFrame.as_matrix`` is deprecated. Use ``NDFrame.values`` instead (:issue:`18458`).
8686
-
8787

8888
.. _whatsnew_0220.prior_deprecations:

pandas/core/generic.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -3735,6 +3735,9 @@ def _get_bool_data(self):
37353735

37363736
def as_matrix(self, columns=None):
37373737
"""
3738+
DEPRECATED: This method will be removed in a future version.
3739+
Use :meth:`NDFrame.values` instead.
3740+
37383741
Convert the frame to its Numpy-array representation.
37393742
37403743
Parameters
@@ -3770,6 +3773,8 @@ def as_matrix(self, columns=None):
37703773
--------
37713774
pandas.DataFrame.values
37723775
"""
3776+
warnings.warn("This method will be removed in a future version. "
3777+
"Use ``.values`` instead.")
37733778
self._consolidate_inplace()
37743779
if self._AXIS_REVERSED:
37753780
return self._data.as_matrix(columns).T
@@ -3791,7 +3796,10 @@ def values(self):
37913796
int32. By numpy.find_common_type convention, mixing int64 and uint64
37923797
will result in a flot64 dtype.
37933798
"""
3794-
return self.as_matrix()
3799+
self._consolidate_inplace()
3800+
if self._AXIS_REVERSED:
3801+
return self._data.as_matrix().T
3802+
return self._data.as_matrix()
37953803

37963804
@property
37973805
def _values(self):
@@ -3801,11 +3809,11 @@ def _values(self):
38013809
@property
38023810
def _get_values(self):
38033811
# compat
3804-
return self.as_matrix()
3812+
return self.values
38053813

38063814
def get_values(self):
38073815
"""same as values (but handles sparseness conversions)"""
3808-
return self.as_matrix()
3816+
return self.values
38093817

38103818
def get_dtype_counts(self):
38113819
"""Return the counts of dtypes in this object."""

pandas/tests/frame/test_api.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ def test_itertuples(self):
243243
def test_len(self):
244244
assert len(self.frame) == len(self.frame.index)
245245

246-
def test_as_matrix(self):
246+
def test_values(self):
247247
frame = self.frame
248-
mat = frame.as_matrix()
248+
mat = frame.values
249249

250250
frameCols = frame.columns
251251
for i, row in enumerate(mat):
@@ -261,11 +261,11 @@ def test_as_matrix(self):
261261
assert mat[0, 0] == 'bar'
262262

263263
df = self.klass({'real': [1, 2, 3], 'complex': [1j, 2j, 3j]})
264-
mat = df.as_matrix()
264+
mat = df.values
265265
assert mat[0, 0] == 1j
266266

267267
# single block corner case
268-
mat = self.frame.as_matrix(['A', 'B'])
268+
mat = self.frame[['A', 'B']].values
269269
expected = self.frame.reindex(columns=['A', 'B']).values
270270
assert_almost_equal(mat, expected)
271271

@@ -311,8 +311,8 @@ def test_class_axis(self):
311311
DataFrame.index # no exception!
312312
DataFrame.columns # no exception!
313313

314-
def test_more_asMatrix(self):
315-
values = self.mixed_frame.as_matrix()
314+
def test_more_values(self):
315+
values = self.mixed_frame.values
316316
assert values.shape[1] == len(self.mixed_frame.columns)
317317

318318
def test_repr_with_mi_nat(self):

pandas/tests/frame/test_nonunique_indexes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def test_columns_with_dups(self):
439439
xp.columns = ['A', 'A', 'B']
440440
assert_frame_equal(rs, xp)
441441

442-
def test_as_matrix_duplicates(self):
442+
def test_values_duplicates(self):
443443
df = DataFrame([[1, 2, 'a', 'b'],
444444
[1, 2, 'a', 'b']],
445445
columns=['one', 'one', 'two', 'two'])

pandas/tests/sparse/test_frame.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ def test_fill_value_when_combine_const(self):
7878
res = df.add(2, fill_value=0)
7979
tm.assert_sp_frame_equal(res, exp)
8080

81-
def test_as_matrix(self):
82-
empty = self.empty.as_matrix()
81+
def test_values(self):
82+
empty = self.empty.values
8383
assert empty.shape == (0, 0)
8484

8585
no_cols = SparseDataFrame(index=np.arange(10))
86-
mat = no_cols.as_matrix()
86+
mat = no_cols.values
8787
assert mat.shape == (10, 0)
8888

8989
no_index = SparseDataFrame(columns=np.arange(10))
90-
mat = no_index.as_matrix()
90+
mat = no_index.values
9191
assert mat.shape == (0, 10)
9292

9393
def test_copy(self):

0 commit comments

Comments
 (0)