-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
DEPR: Deprecate NDFrame.as_matrix #18458
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -3670,19 +3670,36 @@ 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_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: | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks a lot, The issue was actually in the 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): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,31 +243,31 @@ def test_itertuples(self): | |
def test_len(self): | ||
assert len(self.frame) == len(self.frame.index) | ||
|
||
def test_as_matrix(self): | ||
def test_values(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you leave one test (or now add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I've added in below |
||
frame = self.frame | ||
mat = frame.as_matrix() | ||
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.as_matrix(['foo', 'A']) | ||
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.as_matrix() | ||
assert mat[0, 0] == 1j | ||
arr = df.values | ||
assert arr[0, 0] == 1j | ||
|
||
# single block corner case | ||
mat = self.frame.as_matrix(['A', 'B']) | ||
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 | ||
|
@@ -311,8 +311,8 @@ def test_class_axis(self): | |
DataFrame.index # no exception! | ||
DataFrame.columns # no exception! | ||
|
||
def test_more_asMatrix(self): | ||
values = self.mixed_frame.as_matrix() | ||
def test_more_values(self): | ||
values = self.mixed_frame.values | ||
assert values.shape[1] == len(self.mixed_frame.columns) | ||
|
||
def test_repr_with_mi_nat(self): | ||
|
@@ -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(columns=self.frame.columns.tolist()) | ||
expected = self.frame.values | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
def test_deepcopy(self): | ||
cp = deepcopy(self.frame) | ||
series = cp['A'] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just return
.values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the def of
.values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, then we should push this entirely down to the BlockManager, so pass the axis as well. The BM can then do the transpose, we don't like to do things in user code like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should actually deprecate
.values
as well, in favor of.to_numpy()
which is the future spelling anyhow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already almost entirely pushed down, since it is calling directly BlockManager.as_matrix (but OK, the transpose could be done inside
BlockManager.as_matrix
). Theconsolidate_inplace
needs to happens in NDFrame I think.Deprecating
.values
is a no-go IMO, but let's deprecate that in another issue if you wantThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too familiar with the BlockManager, except accessing it with
._data
. How do I pass the axis to it, and how do I transpose in a BlockManager? E.g. it doesn't have a.transpose
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
look at
_take
inpandas/core/generic.py
, you just pass theget_block_manager_axis
to it.then add
axis=
todef as_matrix
inpandas/core/internals
and do the transpose there.The reason for pushing this to internals is to avoid all of the wrapping layers (e.g. frame/series, etc.) to know about internal details like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as_matrix
doesn't have a axis parameter. Do you mean adding a new axis parameter there and transposing inside, ifaxis=0
(where I assume from my limited knowledge thatBlockManager.axes[0]
is always the same asdataFrame.axes[1]
, correct?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jreback, I've tried quite a bit today and I can't push this down while getting the tests to pass.
Could you look at my latest commit (not passing ATM) and give some advice?