Search code examples
pythonnumpy

FutureWarning: 'DataFrame.swapaxes' is deprecated and will be removed in a future version


Looks like numpy is using deprecated function DataFrame.swapaxes in fromnumeric.py.

Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:59: FutureWarning: 'DataFrame.swapaxes' is deprecated and will be removed in a future version. Please use 'DataFrame.transpose' instead.
  return bound(*args, **kwds)

I am getting this warning from the following line of code in Jupyter Notebook:

train, val, test = np.split(df.sample(frac=1), [int(0.8*len(df)), int(0.9*len(df))])

This is the structure of the dataframe I am using: enter image description here

What exactly is raising this warning and what should I change in my code to get rid of this Warning?

I also found that this is currently an open issue of numpy in github. It will be great if anybody could help. Thanks in advance.


Solution

  • According to the numpy issue on github, this "bug" will not be fixed in numpy. The official statement is that np.split should not be used to split pandas DataFrames anymore. Instead, iloc should be used to split DataFrames as it is described in this answer.

    As it looks that you are splitting the DataFrame for machine learning reasons, please be aware that there are built-in methods in many machine learning libraries doing the test/train(/validation) split for you. Here an the example of sklearn.