Skip to content

Commit

Permalink
updating according to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tp committed Nov 25, 2017
1 parent 6b71d4a commit eed2fb6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
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. "
"Use ``values`` instead.", FutureWarning, stacklevel=2)
self._consolidate_inplace()
if self._AXIS_REVERSED:
return self._data.as_matrix(columns).T
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:
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:
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):
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 comments on commit eed2fb6

Please sign in to comment.