Skip to content

DEPR: Deprecate NDFrame.as_matrix #18458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
rename BlockManager.as_matrix to .as_array
  • Loading branch information
tp committed Nov 26, 2017
commit 48e1fc84811aba2831c38f2db20102bdf8ed8c4e
11 changes: 5 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should just return self.values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning self.values implies that columns=None which isn't necessary true for user code.

return self._data.as_matrix(columns)
return self._data.as_array(transpose=self._AXIS_REVERSED,
items=columns)

@property
def values(self):
Expand All @@ -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):
Expand Down
18 changes: 16 additions & 2 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a doc-string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

arr = np.empty(self.shape, dtype=float)
return arr.transpose() if transpose else arr
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,29 +245,29 @@ def test_len(self):

def test_values(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave one test (or now add a test_as_matrix_deprecated that copies eg the first case of this test) that uses as_matrix and that asserts it raises a warning and is the same as .values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I've added in below test_repr_with_mi_nat

frame = self.frame
mat = frame.values
arr = frame.values

frameCols = frame.columns
for i, row in enumerate(mat):
frame_cols = frame.columns
for i, row in enumerate(arr):
for j, value in enumerate(row):
col = frameCols[j]
col = frame_cols[j]
if np.isnan(value):
assert np.isnan(frame[col][i])
else:
assert value == frame[col][i]

# mixed type
mat = self.mixed_frame[['foo', 'A']].values
assert mat[0, 0] == 'bar'
arr = self.mixed_frame[['foo', 'A']].values
assert arr[0, 0] == 'bar'

df = self.klass({'real': [1, 2, 3], 'complex': [1j, 2j, 3j]})
mat = df.values
assert mat[0, 0] == 1j
arr = df.values
assert arr[0, 0] == 1j

# single block corner case
mat = self.frame[['A', 'B']].values
arr = self.frame[['A', 'B']].values
expected = self.frame.reindex(columns=['A', 'B']).values
assert_almost_equal(mat, expected)
assert_almost_equal(arr, expected)

def test_transpose(self):
frame = self.frame
Expand Down Expand Up @@ -372,7 +372,7 @@ def test_values(self):
def test_as_matrix_deprecated(self):
# GH18458
with tm.assert_produces_warning(FutureWarning):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

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)

Expand Down
82 changes: 41 additions & 41 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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])

Expand Down Expand Up @@ -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])

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down