Python Forum
to_numpy() works in jupyter notebook, but not in python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
to_numpy() works in jupyter notebook, but not in python script
#1
Hi

I am unsure why the following works in a jupyter notebook, but not in a python script that I am run from the Linux command line:

#!/usr/bin/python3
import pandas as pd
import numpy as np

# SWAPPING COLUMNS

dates=pd.date_range('1/1/2019',periods=12)
df=pd.DataFrame(np.random.randn(12,4),index=dates,columns=['A','B','C','D'])

df_copy=df.copy()
# assign, after converting to raw data
df_copy.loc[: ['B', 'A']]=df_copy[['A','B']].to_numpy()
print(df_copy)
Any suggestions ?
Reply
#2
List errors .
Reply
#3
As it could be seen from Pandas official docs .to_numpy was added in v.0.24.0. You are likely using different versions when executing a script and ipynb-file.
Reply
#4
to_numpy works fine.
dfn = df[['B', 'A']].to_numpy(copy=True)
This line of code doesn't work for me.
# assign, after converting to raw data
df_copy.loc[: ['B', 'A']]=df_copy[['A','B']].to_numpy()
Reply
#5
It is very important to post your error messages! I see you are missing a comma in .loc, should be loc[:, ...]. And, what are you trying to do? Switch columns order? You can access underlying numpy array by df.values.
Reply
#6
Thanks for spotting the missing comma.
The error that appears is as follows:

Traceback (most recent call last):
  File "./dataframe_swap_columns.py", line 23, in <module>
    df_copy.loc[:, ['B', 'A']]=df_copy[['A','B']].to_numpy()
  File "/usr/lib/python3/dist-packages/pandas/core/generic.py", line 4378, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'to_numpy'
But as I mentioned, it works in a jupyter notebook.
How can I print the version of pandas in a Python script?
Maybe that is the issue.
Reply
#7
import pandas as pd

pd.__version__
As mention by @scidam you need 0.24 --> for .to_numpy() to work.
I guess if you just remove .to_numpy() it will work on earlier version.

Upgrade:
pip3 install --upgrade pandas
Or as me i have pip point to Python 3.7.
Test from command line with -V
tom@tom:~$ python -V
Python 3.7.3

tom@tom:~$ pip -V
pip 19.1 from /home/tom/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pip (python 3.7)
Reply
#8
I have 0.23 on my Ubuntu Linux, but 0.24 on my miniconda environment.

I used sudo apt-get install in an attempt to update pandas to 0.24, but I get this message:

$ sudo apt-get install python3-pandas
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-pandas is already the newest version (0.23.3+dfsg-3ubuntu1).
The following packages were automatically installed and are no longer required:
..
....
cosmic
Use 'sudo apt autoremove' to remove them.
0 to upgrade, 0 to newly install, 0 to remove and 3 not to upgrade.
So it won't update to 0.24.
At least I know what the issue now.
Thanks for your help.

thanks for the upgrade info.
I had to install pip first using sudo apt-get install python3-pip, and then I was able to
"$ pip3 install --upgrade pandas".
So now the script works.
thanks to everybody for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HELP! Importing json file into csv into jupyter notebook vilsef 2 142 Jan-22-2021, 11:06 AM
Last Post: snippsat
  How can draw a real-time marker on map using folium/leaflet Jupyter notebook C3PO 0 124 Dec-22-2020, 07:04 PM
Last Post: C3PO
  Unable to install dataprep on my python notebook simplexity 0 276 Oct-11-2020, 01:59 AM
Last Post: simplexity
  Python Custom Module not working in Jupyter Notebook with Pandas fid 0 338 Jul-04-2020, 11:05 AM
Last Post: fid
  How to change working directory in Jupyter Notebook? rajeev1729 2 12,835 Aug-25-2019, 07:25 PM
Last Post: perfringo
  Automatically updated graph in jupyter notebook using qt5 capponero 0 2,367 May-16-2017, 03:08 PM
Last Post: capponero

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020