How to get definite answer instead of nan in Python?
I need help to understand the output of this code. Why am I getting Nan instead of float value? Please suggest necessary amendments require:
import matplotlib.pyplot as plt
from scipy import stats
import pandas as pd
import fix_yahoo_finance as fyf
from pandas_datareader import data as pdr
import numpy as np
fyf.pdr_override()
p=pdr.get_data_yahoo('IBM',start ='2009-01-01',end ='2013-01-01')
p.to_csv('YF_IBM_2009_2013.csv')
print(p.head())
ret = (p.Close[1:]-p.Close[:-1])/p.Close[1:]
print ('ticker=','IBM','W-test, and P-value')
print (stats.shapiro(ret))
And output is:
ret = (p.Close[1:]-p.Close[:-1])/p.Close[1:]
print ('ticker=','IBM','W-test, and P-value')
print (stats.shapiro(ret))
ticker= IBM W-test, and P-value
(nan, 1.0)
python pandas financial
add a comment |
I need help to understand the output of this code. Why am I getting Nan instead of float value? Please suggest necessary amendments require:
import matplotlib.pyplot as plt
from scipy import stats
import pandas as pd
import fix_yahoo_finance as fyf
from pandas_datareader import data as pdr
import numpy as np
fyf.pdr_override()
p=pdr.get_data_yahoo('IBM',start ='2009-01-01',end ='2013-01-01')
p.to_csv('YF_IBM_2009_2013.csv')
print(p.head())
ret = (p.Close[1:]-p.Close[:-1])/p.Close[1:]
print ('ticker=','IBM','W-test, and P-value')
print (stats.shapiro(ret))
And output is:
ret = (p.Close[1:]-p.Close[:-1])/p.Close[1:]
print ('ticker=','IBM','W-test, and P-value')
print (stats.shapiro(ret))
ticker= IBM W-test, and P-value
(nan, 1.0)
python pandas financial
3
Please reformat as code instead of quotation.
– Mad Physicist
Nov 14 '18 at 4:48
add a comment |
I need help to understand the output of this code. Why am I getting Nan instead of float value? Please suggest necessary amendments require:
import matplotlib.pyplot as plt
from scipy import stats
import pandas as pd
import fix_yahoo_finance as fyf
from pandas_datareader import data as pdr
import numpy as np
fyf.pdr_override()
p=pdr.get_data_yahoo('IBM',start ='2009-01-01',end ='2013-01-01')
p.to_csv('YF_IBM_2009_2013.csv')
print(p.head())
ret = (p.Close[1:]-p.Close[:-1])/p.Close[1:]
print ('ticker=','IBM','W-test, and P-value')
print (stats.shapiro(ret))
And output is:
ret = (p.Close[1:]-p.Close[:-1])/p.Close[1:]
print ('ticker=','IBM','W-test, and P-value')
print (stats.shapiro(ret))
ticker= IBM W-test, and P-value
(nan, 1.0)
python pandas financial
I need help to understand the output of this code. Why am I getting Nan instead of float value? Please suggest necessary amendments require:
import matplotlib.pyplot as plt
from scipy import stats
import pandas as pd
import fix_yahoo_finance as fyf
from pandas_datareader import data as pdr
import numpy as np
fyf.pdr_override()
p=pdr.get_data_yahoo('IBM',start ='2009-01-01',end ='2013-01-01')
p.to_csv('YF_IBM_2009_2013.csv')
print(p.head())
ret = (p.Close[1:]-p.Close[:-1])/p.Close[1:]
print ('ticker=','IBM','W-test, and P-value')
print (stats.shapiro(ret))
And output is:
ret = (p.Close[1:]-p.Close[:-1])/p.Close[1:]
print ('ticker=','IBM','W-test, and P-value')
print (stats.shapiro(ret))
ticker= IBM W-test, and P-value
(nan, 1.0)
python pandas financial
python pandas financial
edited Nov 16 '18 at 20:35
halfer
14.5k758111
14.5k758111
asked Nov 14 '18 at 4:46
Himanshu DoneriaHimanshu Doneria
123
123
3
Please reformat as code instead of quotation.
– Mad Physicist
Nov 14 '18 at 4:48
add a comment |
3
Please reformat as code instead of quotation.
– Mad Physicist
Nov 14 '18 at 4:48
3
3
Please reformat as code instead of quotation.
– Mad Physicist
Nov 14 '18 at 4:48
Please reformat as code instead of quotation.
– Mad Physicist
Nov 14 '18 at 4:48
add a comment |
1 Answer
1
active
oldest
votes
There is a small issue with your code. When you directly subtract two pandas series, the index comes along. Below is the output for
p.Close[1:]
Having index along with values is the reason you're getting nan values. To select only the values from a pandas series, you have to do
p.Close[1:].values
so the ret = line now is
ret = ((p.Close[1:].values-p.Close[:-1].values)/(p.Close[1:].values))
This should do what you're looking for. Comment if anything else is needed.
Thanks, It's suffice the purpose.
– Himanshu Doneria
Nov 14 '18 at 7:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293338%2fhow-to-get-definite-answer-instead-of-nan-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a small issue with your code. When you directly subtract two pandas series, the index comes along. Below is the output for
p.Close[1:]
Having index along with values is the reason you're getting nan values. To select only the values from a pandas series, you have to do
p.Close[1:].values
so the ret = line now is
ret = ((p.Close[1:].values-p.Close[:-1].values)/(p.Close[1:].values))
This should do what you're looking for. Comment if anything else is needed.
Thanks, It's suffice the purpose.
– Himanshu Doneria
Nov 14 '18 at 7:43
add a comment |
There is a small issue with your code. When you directly subtract two pandas series, the index comes along. Below is the output for
p.Close[1:]
Having index along with values is the reason you're getting nan values. To select only the values from a pandas series, you have to do
p.Close[1:].values
so the ret = line now is
ret = ((p.Close[1:].values-p.Close[:-1].values)/(p.Close[1:].values))
This should do what you're looking for. Comment if anything else is needed.
Thanks, It's suffice the purpose.
– Himanshu Doneria
Nov 14 '18 at 7:43
add a comment |
There is a small issue with your code. When you directly subtract two pandas series, the index comes along. Below is the output for
p.Close[1:]
Having index along with values is the reason you're getting nan values. To select only the values from a pandas series, you have to do
p.Close[1:].values
so the ret = line now is
ret = ((p.Close[1:].values-p.Close[:-1].values)/(p.Close[1:].values))
This should do what you're looking for. Comment if anything else is needed.
There is a small issue with your code. When you directly subtract two pandas series, the index comes along. Below is the output for
p.Close[1:]
Having index along with values is the reason you're getting nan values. To select only the values from a pandas series, you have to do
p.Close[1:].values
so the ret = line now is
ret = ((p.Close[1:].values-p.Close[:-1].values)/(p.Close[1:].values))
This should do what you're looking for. Comment if anything else is needed.
edited Nov 14 '18 at 6:44
answered Nov 14 '18 at 5:57
gauravtolanigauravtolani
564
564
Thanks, It's suffice the purpose.
– Himanshu Doneria
Nov 14 '18 at 7:43
add a comment |
Thanks, It's suffice the purpose.
– Himanshu Doneria
Nov 14 '18 at 7:43
Thanks, It's suffice the purpose.
– Himanshu Doneria
Nov 14 '18 at 7:43
Thanks, It's suffice the purpose.
– Himanshu Doneria
Nov 14 '18 at 7:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293338%2fhow-to-get-definite-answer-instead-of-nan-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Please reformat as code instead of quotation.
– Mad Physicist
Nov 14 '18 at 4:48