Reputation: 13
I currently use the .as_matrix() function in order to ensure that the numpy array keeps the correct column order; however this is due to be depreciated. How can I ensure the column order is maintained once the as_matrix function is depreciated by using the suggested .values function? Or is there another method?
Many thanks
Example with as_matrix
In: prices.pct_change()[1:].as_matrix(stocks)
Out: array([-0.00283364, 0.0012285 , 0.0014199 , 0.00142983, -0.0053432 ])
Example with Values
In: prices.pct_change()[1:].values
Out: array([ 0.00142983, 0.0014199 , -0.00283364, -0.0053432 , 0.0012285 ])
Upvotes: 1
Views: 4992
Reputation: 40918
As you point out, .as_matrix()
is deprecated. (See below for comparison to .values
.)
Regardless, seems like you can get the columns in respective order by using .loc
first:
import pandas as pd
import numpy as np
np.random.seed(444)
prices = pd.DataFrame(np.random.randn(200, 4), columns=list('abcd'))
columns = list('cad')
prices.pct_change().dropna().loc[:, columns].values
Here's the source for .as_matrix()
versus .values
. You'll notice only a slight difference:
def as_matrix(self, columns=None):
warnings.warn("Method .as_matrix will be removed in a future version. "
"Use .values instead.", FutureWarning, stacklevel=2)
self._consolidate_inplace()
return self._data.as_array(transpose=self._AXIS_REVERSED,
items=columns)
@property
def values(self):
self._consolidate_inplace()
return self._data.as_array(transpose=self._AXIS_REVERSED)
Hence if you really wanted to, you could just recreate .as_matrix()
without the warning. (But I would strongly prefer the first method; it's the public API; it doesn't make you deal with Pandas internal Block
type yourself.)
chg = prices.pct_change().dropna()
val = chg._data.as_array(transpose=chg._AXIS_REVERSED, items=columns)
assert np.allclose(val, prices.pct_change().dropna().loc[:, columns].values)
Upvotes: 3