Skip to content Skip to sidebar Skip to footer

Pandas As_matrix() To Values Keep Column Order

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

Solution 1:

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:

defas_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)

@propertydefvalues(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)

Post a Comment for "Pandas As_matrix() To Values Keep Column Order"