2

I am trying to plot histogram of percentage change in stock my code looks like:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv("M:/Trading/1.JOZO/ALXN.csv")
dataframe = (data['Adj Close'])
zmena1 = (dataframe.pct_change(periods = 1)*100)
data["Zmena"] = zmena1
plt.hist(zmena1, bins = "auto", range = "auto" )
plt.show

but i get an error:

 mn, mx = [mi + 0.0 for mi in range]

TypeError: Can't convert 'float' object to str implicitly

I tried str(zmena1) but can to get it... Do not know how to move through this one...

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
jjj
  • 35
  • 1
  • 8
  • Please include a sample of your data to create a [mcve](http://stackoverflow.com/help/mcve). Read [how-to-make-good-reproducible-pandas-examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Julien Marrec Dec 12 '16 at 16:29

1 Answers1

3

From the name of the csv file, I can guess that your data can be retrieved from Yahoo finance, so using the Remote Access datareader I'm downloading all 2016 data to play with:

import datetime
data = web.DataReader('ALXN', data_source='yahoo', 
                      start=datetime.datetime(2016, 1, 1))

Now I can calculate the percent change in the [0,100] range

data['Zmena'] = data['Adj Close'].pct_change(periods=1)*100

From there, I would definitely use the built-in DataFrame.hist function:

data['Zmena'].hist()

Histogram


Using plt.hist

In case you do want to use plt.hist instead, you need to filter out the NaN(not a number), in particular you will always have one as the first entry:

print(data[['Adj Close','Zmena']].head())

             Adj Close     Zmena
Date                            
2016-01-04  184.679993       NaN
2016-01-05  184.899994  0.119126
2016-01-06  184.070007 -0.448884
2016-01-07  174.369995 -5.269741
2016-01-08  168.130005 -3.578592

So, in order to use plt.hist:

plt.hist(data.Zmena.dropna())

Another problem is that you're specifying bins = "auto", range = "auto", when really you should just not pass them if you want to default. See the documentation for both parameters at pyplot.hist

Julien Marrec
  • 11,605
  • 4
  • 46
  • 63
  • this is it sir.... somehow as a newbie forgot about this built in function... very thank you – jjj Dec 12 '16 at 16:44