How can I access IPython's "display" function? - python

I tried this code, expecting it to use IPython's display function:
import pandas as pd
data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets'])
display(data.head(10))
But I get an error message that says NameError: name 'display' undefined. Why? How do I make it so that I can use display?

display is a function in the IPython.display module that runs the appropriate dunder method to get the appropriate data to ... display. If you really want to run it
from IPython.display import display
import pandas as pd
data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets'])
display(data.head(10))
But don't. IPython is already doing that for you. Just do:
data.head(10)
You even might have IPython uninstalled, try:
pip install IPython
or if running pip3:
pip3 install IPython

To solve the problem on pycaret, you have to open the below file -
..\env\Lib\site-packages\pycaret\datasets.py
and add the line of code -
from IPython.display import display

Related

I can't read excel file using dt.fread from datatable AttributeError

Hello I'm trying to read an excel file 'myFile.xlsx' using datatable.fread (version 1.0.0) function to speedup data manipulation.
The problem is I had an AttributeError: module 'xlrd' has no attribute 'xlsx'.
The command I used is:
import datatable as dt
DT = dt.fread("myFile.xlsx")
I checked the module where the error occurred is the module xls of datatable package:
def read_xls_workbook(filename, subpath):
try:
import xlrd
# Fixes the warning
# "PendingDeprecationWarning: This method will be removed in future
# versions. Use 'tree.iter()' or 'list(tree.iter())' instead."
xlrd.xlsx.ensure_elementtree_imported(False, None) # Here
xlrd.xlsx.Element_has_iter = True # and Here
Is there any solution to fix this issue? please.
The issue is that datatable package is not updated yet to make use of xldr>1.2.0, so in order to make it work you have to install xldr = 1.2.0
pip install xldr==1.2.0
I hope it helped.

On running this python code in GOOGLE COLAB, it showing me error. Can anyone please tell what am i doing wrong and share a corrected code?

import pandas_datareader.data as web
import numpy as np
import pandas as pd
stock = ['AAPL']
data = web.DataReader(stock,data_source="yahoo",start='01/01/2010')['Adj Close']
data.sort_index(inplace=True)
returns = data.pct_change()
mean_return = returns.mean()
return_stdev = returns.std()
annualised_return = round(mean_return * 252,2)
annualised_stdev = round(return_stdev * np.sqrt(252),2)
print ('The annualised mean return of stock {} is {}, '
'and the annualised volatility is {}').format(stock[0],annualised_return,annualised_stdev)
Release notes for pandas_datareader 0.10.0 does mention that there were issues with Yahoo API's requiring headers and were fixed in this release (0.10.0).
Fixed Yahoo readers which now require headers
So, if your google-colab uses any version older than 0.10.0 will have problems using Yahoo API's.
Here are some steps on how to debug the issue in google-colab - jupyter notebook.
Step 1: Determine the installed version of module pandas_datareader.
!pip show pandas_datareader
Step 2: if the version is lower than 0.10.0 then upgrade the version.
!pip install --upgrade pandas_datareader
Step 3: Don't forget to restart the runtime to load the new libraries.
Press Runtime->Restart runtime
Step 4: Now try running the step 1 again to determine if the newest version is installed.
!pip show pandas_datareader
I hope the newest version will be installed and you can run your above code with the correction mentioned by #Aaron in his answer.
Note: ! is required before shell commands. Try going thorugh the official docs of IPython for more info.
You're calling format on the output of the print function by putting it after the close parenthesis. print doesn't return anything, so you're effectively calling None.format(...) (which doesn't exist). You should instead call format on the string directly as such:
print('The annualised mean return of stock {} is {}, and the annualised volatility is {}'.format(stock[0],annualised_return,annualised_stdev))
Here is the corrected code:
import pandas_datareader.data as web
import numpy as np
import pandas as pd
stock = ['AAPL']
data = web.DataReader(stock,data_source="yahoo",start='01/01/2010')['Adj Close']
data.sort_index(inplace=True)
returns = data.pct_change()
mean_return = returns.mean()
return_stdev = returns.std()
annualised_return = round(mean_return * 252,2)
annualised_stdev = round(return_stdev * np.sqrt(252),2)
print(f"The annualised mean return of stock {stock[0]} is {annualised_return}, and the annualised volatility is {annualised_stdev}")
So the error came because you did not use a f string. An f string puts data in-between the brackets{}
For more info visit:
https://www.geeksforgeeks.org/python-output-formatting/#:~:text=In%20Python%2C%20there%20is%20no%20printf%20%28%29%20function,string%20modulo%20%28or%20sometimes%20even%20called%20modulus%29%20operator.

How to convert a pandas plot into an image

I am working on an app which will be able to show a graph of the company's performance in stocks, I wanted to turn the pandas plot of that company into an image without saving it. Can someone tell me what to do?
from fastquant import get_pse_data
import matplotlib.pyplot as plt
import pandas as pd
df = get_pse_data(symbol, '2019-01-01', '2020-01-01')
ma30 = df.close.rolling(30).mean()
close_ma30 = pd.concat([df.close, ma30], axis=1).dropna()
I am actually thinking of adding this plot derived from a pandas dataframe close_ma30 = pd.concat([df.close, ma30], axis=1).dropna() into my html code:
I want to create a python function that will allow me to return it as an image for a django code. Thank you for the help!
You can use Dataframe-image to convert a pandas plot into a image, you can Visit https://pypi.org/project/dataframe-image/.
dataframe_image has the ability to export both normal and styled DataFrames as images from within a Python script. Pass your normal or styled DataFrame to the export function along with a file location to save it as an image.
>>> import dataframe_image as dfi
>>> dfi.export(df_styled, 'df_styled.png')
You may also export directly from the DataFrame or styled DataFrame using the dfi.export and export_png methods, respectively.
>>> df.dfi.export('df.png')
>>> df_styled.export_png('df_styled.png)
As a Python Library
Dataframe_image can also be used outside of the notebook as a normal Python library. In a separate Python script, import the dataframe_image package and pass the file name of your notebook to the convert function.
>>> import dataframe_image as dfi
>>> dfi.convert('path/to/your_notebook.ipynb',
to='pdf',
use='latex',
center_df=True,
max_rows=30,
max_cols=10,
execute=False,
save_notebook=False,
limit=None,
document_name=None,
table_conversion='chrome'
chrome_path=None,
latex_command=None,
output_dir=None,
)
By default, the new file(s) will be saved in the same directory where the notebook resides. Do not run this command within the same notebook that is being converted.
From the Command Line
The command line tool dataframe_image will be available upon installation with the same options as the convert function from above.
dataframe_image --to=pdf "my notebook with dataframes.ipynb"
Finding Google Chrome
You must have Google Chrome (or Brave) installed in order for dataframe_image to work. The path to Chrome should automatically be found. If Chrome is not in a standard location, set it with the chrome_path parameter.
Using matplotlib instead of Chrome
If you do not have Chrome installed or cannot get it to work properly, you can alternatively use matplotlib to convert the DataFrames to images. Select this option by setting the table_conversion parameter to 'matplotlib'.
Publish to Medium
Closely related to this package is jupyter_to_medium, which publishes your notebooks directly and quickly as Medium blog posts.
Dependencies
You must have the following Python libraries installed.

scikit-surprise: python cannot find module even though pip lists it as installed

I am trying to use the scikit-surprise module to build a recommender system however I am having an error in getting it to compile.
I am receiving the ImportError: Cannot import name "Reader" error
My class is as follows
import pandas as pd
from surprise import Reader, Dataset
userReviewsFilePath ="UserReviewsFirst5000WithHeadings.csv"
ratings = pd.read_csv(userReviewsFilePath) # reading data in pandas df
ratings_dict = {'recipeID': list(ratings.recipeID),
'rating': list(ratings.rating),
'userID': list(ratings.userID)}
df = pd.DataFrame(ratings_dict)
reader = Reader(rating_scale=(1, 5))
data = Dataset.load_from_df(df[['recipeID', 'rating', 'userID']], reader)
pip show says that version 1.0.6 is installed
I think your problem come from the installation... I installed "surprise" and past your code and it worked:
import pandas as pd
from surprise import Reader, Dataset
print(Reader) # or just print(surprise) if you import surprise
out:
<class 'surprise.reader.Reader'>
Start by re-install surprise and tell us.
If you have more than one version of python, do:
which pip
to see if you installed surprise on the used version of python
I think it's in surprise.reader: https://surprise.readthedocs.io/en/stable/reader.html
Your code should read:
from surprise.reader import Reader
from surprise.dataset import Dataset
Edit: I checked the instructions again which seem to contradict this, and give your original code as the correct example. https://surprise.readthedocs.io/en/stable/getting_started.html#getting-started
So maybe they add their own shortcuts? Either way, it seems like this isn't the correct solution, sorry. (Unless it works, in which case their instructions might be out of date.)
Edit 2: They do alias it, so "from surprise import Reader" should indeed have worked: https://github.com/NicolasHug/Surprise/blob/master/surprise/init.py#L19
I think you need to do
from surprise.reader import Reader

python file does not run properly on Spyder

I am using Spyder as my python IDE.
I tried run this python code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
path = os.getcwd() + '\ex1data1.txt'
data = pd.read_csv(path, header = None, names = ['Population', 'Profit'])
data.head()
In theory it is supposed to show me a table of data since I am using
data.head()
at the end
but when I run it, it only shows :
I thought it was supposed to display a new window with the data table.
What is wrong?
You are calling data.head() in .py script. The script is supposed to only read the data, not print it. Try print(data.head())
You want to print your data.head() so do that...
print(data.head())

Categories