Using Pandas documentation
http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.pct_change.html
I am trying to create this function to calculate percentage_change . I pass two paramters to it
def PCT(df,n):
d = df['Close'].pct_change(n)
Even rewriting the same code in different way give me same error
P = pd.Series(df['Close'].pct_change(n), name = 'PCT_' + str(n))
It throwing error
File "D:\Python Scripts\TA_Liabrary.py", line 15, in PCT
d = df['Close'].pct_change(n)
TypeError: 'NoneType' object has no attribute '__getitem__'
Can someone please help me in this
Sample data
Index open high low close volume adj.
1/01/2014 54.97 54.97 54.97 54.97 0 49.31993
2/01/2014 55.1 55.95 54.86 55.08 216100 49.41862
3/01/2014 54.5 55 54.16 55 392600 49.34685
6/01/2014 54.82 55.47 54.62 55.14 344500 49.47245
7/01/2014 55.06 55.17 54.27 54.35 677400 48.76365
8/01/2014 54.64 54.88 53.87 54.38 587500 48.79057
9/01/2014 54.57 54.8 54.05 54.48 466800 48.88029
The pct_change() method returns a DataFrame with the percentage difference between the values for each row and, by default, the previous row. Which row to compare with can be specified with the periods parameter.
get_value() function is used to quickly retrieve the single value in the data frame at the passed column and index. The input to the function is the row label and the column label.
You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.
DataFrame - assign() function The assign() function is used to assign new columns to a DataFrame. Returns a new object with all original columns in addition to new ones. Existing columns that are re-assigned will be overwritten. The column names are keywords.
Apply pct_change
to single/multiple column(s), in a data frame can be done as below
df = pd.DataFrame({
'open': [54.97,55.1,54.5,54.82],
'high': [54.97,55.95,55,55.47],
'low': [54.97,54.86,54.16,54.62],
'close': [54.97,53.08,55,55.14]},
index=['2014-01-01', '2014-02-01', '2014-03-01','2014-04-01'])
open high low close
2014-01-01 54.97 54.97 54.97 54.97
2014-02-01 55.10 55.95 54.86 53.08
2014-03-01 54.50 55.00 54.16 55.00
2014-04-01 54.82 55.47 54.62 55.14
apply pct_change
to a single column (close
)
df.close = df.close.pct_change(periods = 1)
open high low close
2014-01-01 54.97 54.97 54.97 NaN
2014-02-01 55.10 55.95 54.86 -0.034382
2014-03-01 54.50 55.00 54.16 0.036172
2014-04-01 54.82 55.47 54.62 0.002545
applying to multiple columns as below
# apply pct_change to 'open' and 'close'
df[['open','close']] = df[['open','close']].pct_change(periods = 1)
open high low close
2014-01-01 NaN 54.97 54.97 NaN
2014-02-01 0.002365 55.95 54.86 -0.034382
2014-03-01 -0.010889 55.00 54.16 0.036172
2014-04-01 0.005872 55.47 54.62 0.002545
Why can't you use the function as it is in the documents?
a = [10,12,13]
b = [12,11,14]
d = {'open': a, 'close': b}
df = DataFrame(data=d)
print(df)
close open
0 12 10
1 11 12
2 14 13
print(df.pct_change(1))
With a function this will be:
def PCT(dataf,n):
return dataf.pct_change(n)
print(PCT(df, 1))
Both will return:
close open
0 NaN NaN
1 -0.083333 0.200000
2 0.272727 0.083333
And with your sample data PCT(df['close'], 1)
will return:
Index close
2014-01-01 NaN
2014-02-01 0.002001
2014-03-01 -0.001452
2014-06-01 0.002545
2014-07-01 -0.014327
2014-08-01 0.000552
2014-09-01 0.001839
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With