Fabio Magarelli
Fabio Magarelli

Reputation: 1101

Use some Pandas Functions into my Django App

Hi guys I have a problem: I've made my first Django App extensively using pandas in my views.py to load csvs, make some data prep and loading my pickle ML model. The problem is that all this was working fine until I tested on the deployed app (using nginx and uwsgi) where I got an error No Module named pandas which, researching seems to be a very common issue due to the fact that Django doesn't allow importing pandas directly.

I saw some Django-pandas frameworks but their documentation is quite cryptic to me.

Can you explain me in a simple way How can I execute those functions in pandas using Django (even with the help of a Django-pandas framework):

pandas.read_csv()
DataFrame.loc[...]
DataFrame.sort_values()
Series.unique()
Series.size
Series.iloc[0]
DataFrame.from_dict(...)

# would pickle function be affected?
model = pickle.load(open(...))
model.predict()

To make a better example:

import pandas as pd

df = pd.read_csv('...')
df = df.loc[df['...'] == '...']
serie = df['...'].sort_values()
serie = pd.Series(serie.unique())
serie.size
value1 = int(serie.iloc[0])

df = pd.DataFrame.from_dict(dictionary)
model = pickle.load(open(...)) # I don't know if pickle would give problems as well as pandas
prediction = model.predict(df)

UPDATE SOLUTION (Kind of)

So basically I should have followed Shirish Goyal comment and try to use a virtual env. It tooks me all day to solve the problem of the conflict and I may have messed up with the my Ubuntu server. No big deal for me because it is a university project and it's kind of fine as long as it runs but in the future we may want to install a fresh copy of ubuntu.

By the way, the NON destructive useful tips are here: http://ubuntuhandbook.org/index.php/2017/07/install-python-3-6-1-in-ubuntu-16-04-lts/

Just be careful with the last instruction: I accidentally removed /usr/bin/python3.5 folder before I got to that point but after that a lot of Ubuntu dependencies have started acting funny cause Ubuntu makes extensive use of python. If you accidentally delete the python folder you may try running this to repair your ubuntu installation: sudo apt-get install --reinstall ubuntu-desktop. this last command took me 6h to compile and threw me a lot of errors but in the end my Django App is running and that's enough for now. (no more pandas error thrown)

Upvotes: 0

Views: 171

Answers (1)

Shirish Goyal
Shirish Goyal

Reputation: 510

It seems like either you are using virtualenv and pandas seems to be not installed in it or there is conflict in python versions being used.

Have you seen https://www.reddit.com/r/learnpython/comments/9gflmo/import_error_no_module_named_pandas_even_though/ ?

django-pandas is primarily used to interchange django model values to dataframes. I see no reason why pandas should be causing any problems in general.

Upvotes: 1

Related Questions