How to get definite answer instead of nan in Python?












-2















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)









share|improve this question




















  • 3





    Please reformat as code instead of quotation.

    – Mad Physicist
    Nov 14 '18 at 4:48
















-2















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)









share|improve this question




















  • 3





    Please reformat as code instead of quotation.

    – Mad Physicist
    Nov 14 '18 at 4:48














-2












-2








-2








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)









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















0














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:]


enter image description here



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.






share|improve this answer


























  • Thanks, It's suffice the purpose.

    – Himanshu Doneria
    Nov 14 '18 at 7:43













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
});


}
});














draft saved

draft discarded


















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









0














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:]


enter image description here



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.






share|improve this answer


























  • Thanks, It's suffice the purpose.

    – Himanshu Doneria
    Nov 14 '18 at 7:43


















0














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:]


enter image description here



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.






share|improve this answer


























  • Thanks, It's suffice the purpose.

    – Himanshu Doneria
    Nov 14 '18 at 7:43
















0












0








0







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:]


enter image description here



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.






share|improve this answer















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:]


enter image description here



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.







share|improve this answer














share|improve this answer



share|improve this answer








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





















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python