8000 DEPR: Deprecate NDFrame.as_matrix by topper-123 · Pull Request #18458 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

DEPR: Deprecate NDFrame.as_matrix #18458

8000
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
Next Next commit
updating according to comments
  • Loading branch information
tp committed Nov 26, 2017
commit 0d47e46b8aa10ee4c59bfc8abae928dff678bf38
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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. "
Copy link
Contributor

Choose a reason for hiding this comment

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

"method method .as_matrix()...."
"Use .values instead"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I not sure I understand, assume you want the backticks gone. Change uploaded.

"Use ``values`` instead.", FutureWarning, stacklevel=2)
self._consolidate_inplace()
if self._AXIS_REVERSED:
Copy link
Contributor

Choose a reason for hiding this comment

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

ok so this should be the same as below (e.g. passing transpose=). make sure we still have a test on this (e.g. that passes columns).

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, and test_as_matrix_deprecated has been modified to a take columns param.

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.

Expand All @@ -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):
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3670,19 +3670,22 @@ 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:
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

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)
else:
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:
Copy link
Contributor

Choose a reason for hiding this comment

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

change this to transpose instead. should be straightforward from here.

Copy link
Contributor Author
@topper-123 topper-123 Nov 26, 2017

Choose a reason for hiding this comment

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

Thanks a lot, transpose is of course much better than axis.

The issue was actually in the if len(self.blocks) == 0: block, as the empty array also must be transposed.

Everything is green now locally and I've pushed that upstream.

return mgr._interleave()
arr = mgr._interleave()

return arr.transpose() if transpose else arr

def _interleave(self):
"""
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down Expand Up @@ -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):
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()
expected = self.frame.values
tm.assert_numpy_array_equal(result, expected)

def test_deepcopy(self):
cp = deepcopy(self.frame)
series = cp['A']
Expand Down
32 changes: 16 additions & 16 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
0