Skip to content

Commit f26bed6

Browse files
topper-123jreback
authored andcommitted
DEPR: Deprecate NDFrame.as_matrix (#18458)
1 parent f1aac43 commit f26bed6

File tree

9 files changed

+117
-88
lines changed

9 files changed

+117
-88
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Deprecations
8787
~~~~~~~~~~~~
8888

8989
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
90-
-
90+
- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`).
9191
-
9292

9393
.. _whatsnew_0220.prior_deprecations:

pandas/core/generic.py

+11-6
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: as_matrix will be removed in a future version.
3739+
Use :meth:`DataFrame.values` instead.
3740+
37383741
Convert the frame to its Numpy-array representation.
37393742
37403743
Parameters
@@ -3770,10 +3773,11 @@ def as_matrix(self, columns=None):
37703773
--------
37713774
pandas.DataFrame.values
37723775
"""
3776+
warnings.warn("Method .as_matrix will be removed in a future version. "
3777+
"Use .values instead.", FutureWarning, stacklevel=2)
37733778
self._consolidate_inplace()
3774-
if self._AXIS_REVERSED:
3775-
return self._data.as_matrix(columns).T
3776-
return self._data.as_matrix(columns)
3779+
return self._data.as_array(transpose=self._AXIS_REVERSED,
3780+
items=columns)
37773781

37783782
@property
37793783
def values(self):
@@ -3791,7 +3795,8 @@ def values(self):
37913795
int32. By numpy.find_common_type convention, mixing int64 and uint64
37923796
will result in a flot64 dtype.
37933797
"""
3794-
return self.as_matrix()
3798+
self._consolidate_inplace()
3799+
return self._data.as_array(transpose=self._AXIS_REVERSED)
37953800

37963801
@property
37973802
def _values(self):
@@ -3801,11 +3806,11 @@ def _values(self):
38013806
@property
38023807
def _get_values(self):
38033808
# compat
3804-
return self.as_matrix()
3809+
return self.values
38053810

38063811
def get_values(self):
38073812
"""same as values (but handles sparseness conversions)"""
3808-
return self.as_matrix()
3813+
return self.values
38093814

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

pandas/core/internals.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -3484,7 +3484,7 @@ def replace_list(self, src_list, dest_list, inplace=False, regex=False,
34843484
mgr = self
34853485

34863486
# figure out our mask a-priori to avoid repeated replacements
3487-
values = self.as_matrix()
3487+
values = self.as_array()
34883488

34893489
def comp(s):
34903490
if isna(s):
@@ -3670,19 +3670,36 @@ def copy(self, deep=True, mgr=None):
36703670
return self.apply('copy', axes=new_axes, deep=deep,
36713671
do_integrity_check=False)
36723672

3673-
def as_matrix(self, items=None):
3673+
def as_array(self, transpose=False, items=None):
3674+
"""Convert the blockmanager data into an numpy array.
3675+
3676+
Parameters
3677+
----------
3678+
transpose : boolean, default False
3679+
If True, transpose the return array
3680+
items : list of strings or None
3681+
Names of block items that will be included in the returned
3682+
array. ``None`` means that all block items will be used
3683+
3684+
Returns
3685+
-------
3686+
arr : ndarray
3687+
"""
36743688
if len(self.blocks) == 0:
3675-
return np.empty(self.shape, dtype=float)
3689+
arr = np.empty(self.shape, dtype=float)
3690+
return arr.transpose() if transpose else arr
36763691

36773692
if items is not None:
36783693
mgr = self.reindex_axis(items, axis=0)
36793694
else:
36803695
mgr = self
36813696

36823697
if self._is_single_block or not self.is_mixed_type:
3683-
return mgr.blocks[0].get_values()
3698+
arr = mgr.blocks[0].get_values()
36843699
else:
3685-
return mgr._interleave()
3700+
arr = mgr._interleave()
3701+
3702+
return arr.transpose() if transpose else arr
36863703

36873704
def _interleave(self):
36883705
"""

pandas/core/panel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def to_excel(self, path, na_rep='', engine=None, **kwargs):
464464

465465
def as_matrix(self):
466466
self._consolidate_inplace()
467-
return self._data.as_matrix()
467+
return self._data.as_array()
468468

469469
# ----------------------------------------------------------------------
470470
# Getting and setting elements

pandas/tests/frame/test_api.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -243,31 +243,31 @@ 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+
arr = frame.values
249249

250-
frameCols = frame.columns
251-
for i, row in enumerate(mat):
250+
frame_cols = frame.columns
251+
for i, row in enumerate(arr):
252252
for j, value in enumerate(row):
253-
col = frameCols[j]
253+
col = frame_cols[j]
254254
if np.isnan(value):
255255
assert np.isnan(frame[col][i])
256256
else:
257257
assert value == frame[col][i]
258258

259259
# mixed type
260-
mat = self.mixed_frame.as_matrix(['foo', 'A'])
261-
assert mat[0, 0] == 'bar'
260+
arr = self.mixed_frame[['foo', 'A']].values
261+
assert arr[0, 0] == 'bar'
262262

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

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

272272
def test_transpose(self):
273273
frame = self.frame
@@ -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):
@@ -369,6 +369,13 @@ def test_values(self):
369369
self.frame.values[:, 0] = 5.
370370
assert (self.frame.values[:, 0] == 5).all()
371371

372+
def test_as_matrix_deprecated(self):
373+
# GH18458
374+
with tm.assert_produces_warning(FutureWarning):
375+
result = self.frame.as_matrix(columns=self.frame.columns.tolist())
376+
expected = self.frame.values
377+
tm.assert_numpy_array_equal(result, expected)
378+
372379
def test_deepcopy(self):
373380
cp = deepcopy(self.frame)
374381
series = cp['A']

pandas/tests/frame/test_block_internals.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def test_consolidate_inplace(self):
6767
for letter in range(ord('A'), ord('Z')):
6868
self.frame[chr(letter)] = chr(letter)
6969

70-
def test_as_matrix_consolidate(self):
70+
def test_values_consolidate(self):
7171
self.frame['E'] = 7.
7272
assert not self.frame._data.is_consolidated()
73-
_ = self.frame.as_matrix() # noqa
73+
_ = self.frame.values # noqa
7474
assert self.frame._data.is_consolidated()
7575

7676
def test_modify_values(self):
@@ -91,50 +91,50 @@ def test_boolean_set_uncons(self):
9191
self.frame[self.frame > 1] = 2
9292
assert_almost_equal(expected, self.frame.values)
9393

94-
def test_as_matrix_numeric_cols(self):
94+
def test_values_numeric_cols(self):
9595
self.frame['foo'] = 'bar'
9696

97-
values = self.frame.as_matrix(['A', 'B', 'C', 'D'])
97+
values = self.frame[['A', 'B', 'C', 'D']].values
9898
assert values.dtype == np.float64
9999

100-
def test_as_matrix_lcd(self):
100+
def test_values_lcd(self):
101101

102102
# mixed lcd
103-
values = self.mixed_float.as_matrix(['A', 'B', 'C', 'D'])
103+
values = self.mixed_float[['A', 'B', 'C', 'D']].values
104104
assert values.dtype == np.float64
105105

106-
values = self.mixed_float.as_matrix(['A', 'B', 'C'])
106+
values = self.mixed_float[['A', 'B', 'C']].values
107107
assert values.dtype == np.float32
108108

109-
values = self.mixed_float.as_matrix(['C'])
109+
values = self.mixed_float[['C']].values
110110
assert values.dtype == np.float16
111111

112112
# GH 10364
113113
# B uint64 forces float because there are other signed int types
114-
values = self.mixed_int.as_matrix(['A', 'B', 'C', 'D'])
114+
values = self.mixed_int[['A', 'B', 'C', 'D']].values
115115
assert values.dtype == np.float64
116116

117-
values = self.mixed_int.as_matrix(['A', 'D'])
117+
values = self.mixed_int[['A', 'D']].values
118118
assert values.dtype == np.int64
119119

120120
# B uint64 forces float because there are other signed int types
121-
values = self.mixed_int.as_matrix(['A', 'B', 'C'])
121+
values = self.mixed_int[['A', 'B', 'C']].values
122122
assert values.dtype == np.float64
123123

124124
# as B and C are both unsigned, no forcing to float is needed
125-
values = self.mixed_int.as_matrix(['B', 'C'])
125+
values = self.mixed_int[['B', 'C']].values
126126
assert values.dtype == np.uint64
127127

128-
values = self.mixed_int.as_matrix(['A', 'C'])
128+
values = self.mixed_int[['A', 'C']].values
129129
assert values.dtype == np.int32
130130

131-
values = self.mixed_int.as_matrix(['C', 'D'])
131+
values = self.mixed_int[['C', 'D']].values
132132
assert values.dtype == np.int64
133133

134-
values = self.mixed_int.as_matrix(['A'])
134+
values = self.mixed_int[['A']].values
135135
assert values.dtype == np.int32
136136

137-
values = self.mixed_int.as_matrix(['C'])
137+
values = self.mixed_int[['C']].values
138138
assert values.dtype == np.uint8
139139

140140
def test_constructor_with_convert(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'])

0 commit comments

Comments
 (0)