🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 18 (from laksa067)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
CRAWLED
22 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.7 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://quant.stackexchange.com/questions/31094/removing-nan-values-in-python-quantopian
Last Crawled2026-03-22 04:12:11 (22 days ago)
First Indexednot set
HTTP Status Code200
Meta TitleRemoving NaN Values in Python Quantopian - Quantitative Finance Stack Exchange
Meta Descriptionnull
Meta Canonicalnull
Boilerpipe Text
In general you should probably have a look at the following answers: https://stackoverflow.com/a/13434501/604048 https://stackoverflow.com/a/24489602/604048 However, I'd like to address number 3. You should use numpy to check for NaN s, not use the equals operator. np.isnan(...) . You shouldn't use a while loop. It's very unpythonic. Your if statement shouldn't have a parenthesis. The indentation of while part should make this script fail. Remember you're working with rows. You shouldn't get the ambiguous truth value error if your code was implemented the way you showed. Maybe you wrote something like this: i = 0 while (i < len (y)): if np.isnan(y[i]): y[i] = 0 i = i + 1 This produces: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() What is happening here is that the if statement gets a list of bools, and doesn't know what to do with it. Is it true when all of them are true? Or, is it true when any of them are true? You could resolve it as follows: i = 0 while (i < len (y)): if np.isnan(y[i]). any (): y[i] = 0 i = i + 1 Or more pythonic: for i in range ( 0 , len(y)): if np. isnan (y[i]). any (): y[i] = 0 Also be careful about the zero assignment. I don't know how pandas data frames are implemented, but typically with normal collection types you'd be replacing a row with the number zero (0). If we weren't working with special pandas data frames, and it was simply lists I'd go for: y = [ row for row in y if not np.isnan( row ). any ()] ... if you wanted to delete a row with any NaN values. Or, if you simply want to set any element which is NaN to zero: y = [[ 0 if np. isnan (elm) else elm for elm in row] for row in y] With test data: import numpy as np y = [[ 0 , 0 ],[ 1 , 1 ],[ 2 , float ( 'nan' )],[ 3 , 3 ]] print (y) x = [row for row in y if not np.isnan(row). any ()] print (x) z = [[ 0 if np.isnan(elm) else elm for elm in row] for row in y] print (z) This produces the following output: [[0, 0], [1, 1], [2, nan], [3, 3]] [[0, 0], [1, 1], [3, 3]] [[0, 0], [1, 1], [2, 0], [3, 3]]
Markdown
# ![site logo](https://stackoverflow.com/Content/Img/SE-logo75.png) By clicking “Sign up”, you agree to our [terms of service](https://quant.stackexchange.com/legal/terms-of-service/public) and acknowledge you have read our [privacy policy](https://quant.stackexchange.com/legal/privacy-policy). # OR Already have an account? [Log in](https://quant.stackexchange.com/users/login) [Skip to main content](https://quant.stackexchange.com/questions/31094/removing-nan-values-in-python-quantopian#content) #### Stack Exchange Network Stack Exchange network consists of 183 Q\&A communities including [Stack Overflow](https://stackoverflow.com/), the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. [Visit Stack Exchange](https://stackexchange.com/) 1. - [Tour Start here for a quick overview of the site](https://quant.stackexchange.com/tour) - [Help Center Detailed answers to any questions you might have](https://quant.stackexchange.com/help) - [Meta Discuss the workings and policies of this site](https://quant.meta.stackexchange.com/) - [About Us Learn more about Stack Overflow the company, and our products](https://stackoverflow.co/) 2. ### [current community](https://quant.stackexchange.com/) - [Quantitative Finance](https://quant.stackexchange.com/) [help](https://quant.stackexchange.com/help) [chat](https://chat.stackexchange.com/?tab=site&host=quant.stackexchange.com) - [Quantitative Finance Meta](https://quant.meta.stackexchange.com/) ### your communities [Sign up](https://quant.stackexchange.com/users/signup?ssrc=site_switcher&returnurl=https%3A%2F%2Fquant.stackexchange.com%2Fquestions%2F31094%2Fremoving-nan-values-in-python-quantopian) or [log in](https://quant.stackexchange.com/users/login?ssrc=site_switcher&returnurl=https%3A%2F%2Fquant.stackexchange.com%2Fquestions%2F31094%2Fremoving-nan-values-in-python-quantopian) to customize your list. ### [more stack exchange communities](https://stackexchange.com/sites) [company blog](https://stackoverflow.blog/) 3. [Log in](https://quant.stackexchange.com/users/login?ssrc=head&returnurl=https%3A%2F%2Fquant.stackexchange.com%2Fquestions%2F31094%2Fremoving-nan-values-in-python-quantopian) 4. [Sign up](https://quant.stackexchange.com/users/signup?ssrc=head&returnurl=https%3A%2F%2Fquant.stackexchange.com%2Fquestions%2F31094%2Fremoving-nan-values-in-python-quantopian) [![Quantitative Finance](https://quant.stackexchange.com/Content/Sites/quant/Img/logo.svg?v=6edc2035d622)](https://quant.stackexchange.com/) 1. 1. [Home](https://quant.stackexchange.com/) 2. [Questions](https://quant.stackexchange.com/questions) 3. [Unanswered](https://quant.stackexchange.com/unanswered) 4. [AI Assist](https://stackoverflow.com/ai-assist) 5. [Tags](https://quant.stackexchange.com/tags) 6. [Chat](https://chat.stackexchange.com/) 7. [Users](https://quant.stackexchange.com/users) 2. Stack Internal Stack Overflow for Teams is now called **Stack Internal**. Bring the best of human thought and AI automation together at your work. [Try for free](https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=quant-community&utm_campaign=side-bar&utm_content=explore-teams) [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=quant-community&utm_campaign=side-bar&utm_content=explore-teams) 3. [Stack Internal]() 4. Bring the best of human thought and AI automation together at your work. [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=quant-community&utm_campaign=side-bar&utm_content=explore-teams-compact) **Stack Internal** Knowledge at work Bring the best of human thought and AI automation together at your work. [Explore Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=quant-community&utm_campaign=side-bar&utm_content=explore-teams-compact-popover) # [Removing NaN Values in Python Quantopian](https://quant.stackexchange.com/questions/31094/removing-nan-values-in-python-quantopian) [Ask Question](https://quant.stackexchange.com/questions/ask) Asked 9 years, 4 months ago Modified [9 years, 4 months ago](https://quant.stackexchange.com/questions/31094/removing-nan-values-in-python-quantopian?lastactivity "2016-11-21 00:00:34Z") Viewed 9k times This question shows research effort; it is useful and clear \-1 Save this question. Show activity on this post. I am trying to make a histogram in numpy but numpy.histogram seems to really hate NaN values. I have tried removing NaN values from a list called data in three different ways and Quantopian doesn't let me use any of those three ways: 1.) TypeError: only integer arrays with one element can be converted to an index ``` data = data[~np.isnan(data)] ``` 2\.) So then I tried using pandas.dropna() and it threw: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ``` datapd = pd.Series(data) hist, bins = np.histogram(datapd.dropna(), density=True, bins = 'auto') ``` 3\.) And when that didn't work I tried removing them on my own: ``` i = 0 while(i < len(y)): if(float('nan') == y[i]): y[i] = 0 i = i + 1 ``` It threw the same error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() How do you remove NaN values in quantopian? I tried these methods in a python console and it worked I have no clue as to why it wouldn't work in quantopian. - [python](https://quant.stackexchange.com/questions/tagged/python "show questions tagged 'python'") [Share](https://quant.stackexchange.com/q/31094 "Short permalink to this question") Share a link to this question Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this question](https://quant.stackexchange.com/posts/31094/edit) Follow Follow this question to receive notifications asked Nov 20, 2016 at 21:44 [![old-profile's user avatar](https://www.gravatar.com/avatar/f808a989b3aff3ca1e5d711f6afce11b?s=64&d=identicon&r=PG&f=y&so-version=2)](https://quant.stackexchange.com/users/25366/old-profile) [old-profile](https://quant.stackexchange.com/users/25366/old-profile) 10111 silver badge44 bronze badges [Add a comment](https://quant.stackexchange.com/questions/31094/removing-nan-values-in-python-quantopian "Use comments to ask for more information or suggest improvements. Avoid answering questions in comments.") \| ## 1 Answer 1 Sorted by: [Reset to default](https://quant.stackexchange.com/questions/31094/removing-nan-values-in-python-quantopian?answertab=scoredesc#tab-top) This answer is useful 2 Save this answer. Show activity on this post. In general you should probably have a look at the following answers: - <https://stackoverflow.com/a/13434501/604048> - <https://stackoverflow.com/a/24489602/604048> However, I'd like to address number 3. 1. You should use `numpy` to check for `NaN`s, not use the equals operator. `np.isnan(...)`. 2. You shouldn't use a `while` loop. It's very unpythonic. 3. Your if statement shouldn't have a parenthesis. 4. The indentation of `while` part should make this script fail. 5. Remember you're working with rows. You shouldn't get the ambiguous truth value error if your code was implemented the way you showed. Maybe you wrote something like this: ``` i = 0 while(i < len(y)): if np.isnan(y[i]): y[i] = 0 i = i + 1 ``` This produces: > ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() What is happening here is that the if statement gets a list of bools, and doesn't know what to do with it. Is it true when all of them are true? Or, is it true when any of them are true? You could resolve it as follows: ``` i = 0 while(i < len(y)): if np.isnan(y[i]).any(): y[i] = 0 i = i + 1 ``` Or more pythonic: ``` for i in range(0, len(y)): if np.isnan(y[i]).any(): y[i] = 0 ``` Also be careful about the zero assignment. I don't know how pandas data frames are implemented, but typically with normal collection types you'd be replacing a row with the number zero (0). If we weren't working with special pandas data frames, and it was simply lists I'd go for: ``` y = [row for row in y if not np.isnan(row).any()] ``` ... if you wanted to delete a row with any `NaN` values. Or, if you simply want to set any element which is `NaN` to zero: ``` y = [[0 if np.isnan(elm) else elm for elm in row] for row in y] ``` With test data: ``` import numpy as np y = [[0,0],[1,1],[2,float('nan')],[3,3]] print(y) x = [row for row in y if not np.isnan(row).any()] print(x) z = [[0 if np.isnan(elm) else elm for elm in row] for row in y] print(z) ``` This produces the following output: ``` [[0, 0], [1, 1], [2, nan], [3, 3]] [[0, 0], [1, 1], [3, 3]] [[0, 0], [1, 1], [2, 0], [3, 3]] ``` [Share](https://quant.stackexchange.com/a/31097 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://quant.stackexchange.com/posts/31097/edit) Follow Follow this answer to receive notifications [edited May 23, 2017 at 12:41](https://quant.stackexchange.com/posts/31097/revisions "show all edits to this post") [![Community's user avatar](https://www.gravatar.com/avatar/a007be5a61f6aa8f3e85ae2fc18dd66e?s=64&d=identicon&r=PG)](https://quant.stackexchange.com/users/-1/community) [Community](https://quant.stackexchange.com/users/-1/community)Bot 1 answered Nov 21, 2016 at 0:00 [![André Christoffer Andersen's user avatar](https://www.gravatar.com/avatar/3a3794db73b04bdb0e768f2f64c63369?s=64&d=identicon&r=PG)](https://quant.stackexchange.com/users/2792/andr%C3%A9-christoffer-andersen) [André Christoffer Andersen](https://quant.stackexchange.com/users/2792/andr%C3%A9-christoffer-andersen) 27844 silver badges99 bronze badges 0 [Add a comment](https://quant.stackexchange.com/questions/31094/removing-nan-values-in-python-quantopian "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.") \| Start asking to get answers Find the answer to your question by asking. [Ask question](https://quant.stackexchange.com/questions/ask) Explore related questions - [python](https://quant.stackexchange.com/questions/tagged/python "show questions tagged 'python'") See similar questions with these tags. #### Related [1](https://quant.stackexchange.com/questions/14085/calculate-bond-yield-in-python?rq=1 "Question score (upvotes - downvotes)") [Calculate bond yield in python](https://quant.stackexchange.com/questions/14085/calculate-bond-yield-in-python?rq=1) [10](https://quant.stackexchange.com/questions/18094/how-can-i-calculate-the-maximum-drawdown-mdd-in-python?rq=1 "Question score (upvotes - downvotes)") [How can I calculate the Maximum Drawdown MDD in python](https://quant.stackexchange.com/questions/18094/how-can-i-calculate-the-maximum-drawdown-mdd-in-python?rq=1) [0](https://quant.stackexchange.com/questions/19411/need-help-with-donchian-channels-function?rq=1 "Question score (upvotes - downvotes)") [need help with Donchian-channels function\!](https://quant.stackexchange.com/questions/19411/need-help-with-donchian-channels-function?rq=1) [2](https://quant.stackexchange.com/questions/21398/bloomberg-python-question-how-do-you-access-prtu-via-python?rq=1 "Question score (upvotes - downvotes)") [Bloomberg Python Question - How do you access PRTU via python?](https://quant.stackexchange.com/questions/21398/bloomberg-python-question-how-do-you-access-prtu-via-python?rq=1) [2](https://quant.stackexchange.com/questions/24512/quantlib-with-python-on-mac?rq=1 "Question score (upvotes - downvotes)") [Quantlib with python on mac?](https://quant.stackexchange.com/questions/24512/quantlib-with-python-on-mac?rq=1) [0](https://quant.stackexchange.com/questions/26251/python-statsmodel-arma-question?rq=1 "Question score (upvotes - downvotes)") [Python statsmodel ARMA question](https://quant.stackexchange.com/questions/26251/python-statsmodel-arma-question?rq=1) [0](https://quant.stackexchange.com/questions/35292/why-am-i-getting-nan-for-as-the-output-for-a-porfolio-of-505-assets?rq=1 "Question score (upvotes - downvotes)") [Why am I getting 'NaN' for as the output for a porfolio of 505 assets?](https://quant.stackexchange.com/questions/35292/why-am-i-getting-nan-for-as-the-output-for-a-porfolio-of-505-assets?rq=1) [0](https://quant.stackexchange.com/questions/36980/qlyieldtsdiscount-in-quantlib-python?rq=1 "Question score (upvotes - downvotes)") [qlYieldTSDiscount in QuantLib Python](https://quant.stackexchange.com/questions/36980/qlyieldtsdiscount-in-quantlib-python?rq=1) [3](https://quant.stackexchange.com/questions/38329/volatility-target-optimization-python?rq=1 "Question score (upvotes - downvotes)") [Volatility Target Optimization - Python](https://quant.stackexchange.com/questions/38329/volatility-target-optimization-python?rq=1) #### [Hot Network Questions](https://stackexchange.com/questions?tab=hot) - [rapid pronunciation of когда](https://russian.stackexchange.com/questions/29766/rapid-pronunciation-of-%D0%BA%D0%BE%D0%B3%D0%B4%D0%B0) - [What year should be used when citing software without specifying a version?](https://academia.stackexchange.com/questions/226238/what-year-should-be-used-when-citing-software-without-specifying-a-version) - [What is the Tsit5 Butcher Tableau?](https://scicomp.stackexchange.com/questions/45414/what-is-the-tsit5-butcher-tableau) - [Latest openssh server security patch help (1:9.6p1-3ubuntu13.15)](https://askubuntu.com/questions/1564977/latest-openssh-server-security-patch-help-19-6p1-3ubuntu13-15) - [YouTube video metadata and transcript extractor using Python standard library](https://codereview.stackexchange.com/questions/301659/youtube-video-metadata-and-transcript-extractor-using-python-standard-library) - [Trying to understand a statement in a proof about a property related to thrice differentiable functions](https://math.stackexchange.com/questions/5129532/trying-to-understand-a-statement-in-a-proof-about-a-property-related-to-thrice-d) - [Does Exempli Gratia Fecit make sense?](https://latin.stackexchange.com/questions/27126/does-exempli-gratia-fecit-make-sense) - [Composition of canonical embeddings and the resulting "limit" space](https://mathoverflow.net/questions/509336/composition-of-canonical-embeddings-and-the-resulting-limit-space) - [Does Banishment incapacitate a creature sent to its home plane?](https://rpg.stackexchange.com/questions/218974/does-banishment-incapacitate-a-creature-sent-to-its-home-plane) - [Why did the Buddha forbid nuns from criticizing monks but not the other way around? On AN 8.51](https://buddhism.stackexchange.com/questions/55489/why-did-the-buddha-forbid-nuns-from-criticizing-monks-but-not-the-other-way-arou) - [Is there a general formula for the density of a given substance at a given temperature and pressure?](https://physics.stackexchange.com/questions/870319/is-there-a-general-formula-for-the-density-of-a-given-substance-at-a-given-tempe) - [Story written as book reviews of fictional SF books - with a twist](https://scifi.stackexchange.com/questions/303760/story-written-as-book-reviews-of-fictional-sf-books-with-a-twist) - [Obstruction to deforming a cross-section](https://mathoverflow.net/questions/509320/obstruction-to-deforming-a-cross-section) - [How do physicalists define moral "oughts" based on preferences when multiple individuals have incompatible subjective preferences?](https://philosophy.stackexchange.com/questions/137196/how-do-physicalists-define-moral-oughts-based-on-preferences-when-multiple-ind) - [Why does the Professor reverse syllables instead of words or letters?](https://literature.stackexchange.com/questions/31793/why-does-the-professor-reverse-syllables-instead-of-words-or-letters) - [Capacitance of a film capacitor](https://electronics.stackexchange.com/questions/767152/capacitance-of-a-film-capacitor) - [Why does DMARC report SPF failures when SPF appears to pass?](https://serverfault.com/questions/1198526/why-does-dmarc-report-spf-failures-when-spf-appears-to-pass) - [Why does Loge's motive appear at the end of Act 2 of Götterdämmerung?](https://music.stackexchange.com/questions/143383/why-does-loges-motive-appear-at-the-end-of-act-2-of-g%C3%B6tterd%C3%A4mmerung) - [Choosing distribution families when fitting GAMs for different response variables with mgcv](https://stats.stackexchange.com/questions/675265/choosing-distribution-families-when-fitting-gams-for-different-response-variable) - [How do I use the Z3 solver to solve the polyomino rectangle-filling problem?](https://or.stackexchange.com/questions/13532/how-do-i-use-the-z3-solver-to-solve-the-polyomino-rectangle-filling-problem) - [Do any Buddhist texts support a non-theistic grounding for objective moral truths?](https://buddhism.stackexchange.com/questions/55481/do-any-buddhist-texts-support-a-non-theistic-grounding-for-objective-moral-truth) - [Which philosophers argue against treating belief systems as independent entities?](https://philosophy.stackexchange.com/questions/137216/which-philosophers-argue-against-treating-belief-systems-as-independent-entities) - [What is the default keyboard shortcut to split a pane in Microsoft Terminal?](https://superuser.com/questions/1936032/what-is-the-default-keyboard-shortcut-to-split-a-pane-in-microsoft-terminal) - [What's the difference between the Remastered and Complete Edition of Horizon Zero Dawn?](https://gaming.stackexchange.com/questions/418218/whats-the-difference-between-the-remastered-and-complete-edition-of-horizon-zer) [Question feed](https://quant.stackexchange.com/feeds/question/31094 "Feed of this question and its answers") # Subscribe to RSS Question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader. ![](https://quant.stackexchange.com/posts/31094/ivc/d9e7?prg=6630c5a6-d91a-4c66-af7f-4200bf168458) default # Why are you flagging this comment? It contains harassment, bigotry or abuse. This comment attacks a person or group. Learn more in our [Abusive behavior policy](https://quant.stackexchange.com/conduct/abusive-behavior). It's unfriendly or unkind. This comment is rude or condescending. Learn more in our [Code of Conduct](https://quant.stackexchange.com/conduct/abusive-behavior). Not needed. This comment is not relevant to the post. ``` ``` Enter at least 6 characters Something else. A problem not listed above. Try to be as specific as possible. ``` ``` Enter at least 6 characters Flag comment Cancel You have 0 flags left today # ![Illustration of upvote icon after it is clicked](https://quant.stackexchange.com/Content/Img/modal/img-upvote.png?v=fce73bd9724d) # Hang on, you can't upvote just yet. You'll need to complete a few actions and gain 15 reputation points before being able to upvote. **Upvoting** indicates when questions and answers are useful. [What's reputation and how do I get it?](https://stackoverflow.com/help/whats-reputation) Instead, you can save this post to reference later. Save this post for later Not now ##### [Quantitative Finance](https://quant.stackexchange.com/) - [Tour](https://quant.stackexchange.com/tour) - [Help](https://quant.stackexchange.com/help) - [Chat](https://chat.stackexchange.com/?tab=site&host=quant.stackexchange.com) - [Contact](https://quant.stackexchange.com/contact) - [Feedback](https://quant.meta.stackexchange.com/) ##### [Company](https://stackoverflow.co/) - [Stack Overflow](https://stackoverflow.com/) - [Stack Internal](https://stackoverflow.co/internal/) - [Stack Data Licensing](https://stackoverflow.co/data-licensing/) - [Stack Ads](https://stackoverflow.co/advertising/) - [About](https://stackoverflow.co/) - [Press](https://stackoverflow.co/company/press/) - [Legal](https://stackoverflow.com/legal) - [Privacy Policy](https://stackoverflow.com/legal/privacy-policy) - [Terms of Service](https://stackoverflow.com/legal/terms-of-service/public) - Cookie Settings - [Cookie Policy](https://policies.stackoverflow.co/stack-overflow/cookie-policy) ##### [Stack Exchange Network](https://stackexchange.com/) - [Technology](https://stackexchange.com/sites#technology) - [Culture & recreation](https://stackexchange.com/sites#culturerecreation) - [Life & arts](https://stackexchange.com/sites#lifearts) - [Science](https://stackexchange.com/sites#science) - [Professional](https://stackexchange.com/sites#professional) - [Business](https://stackexchange.com/sites#business) - [API](https://api.stackexchange.com/) - [Data](https://data.stackexchange.com/) - [Blog](https://stackoverflow.blog/?blb=1) - [Facebook](https://www.facebook.com/officialstackoverflow/) - [Twitter](https://twitter.com/stackoverflow) - [LinkedIn](https://linkedin.com/company/stack-overflow) - [Instagram](https://www.instagram.com/thestackoverflow) Site design / logo © 2026 Stack Exchange Inc; user contributions licensed under [CC BY-SA](https://stackoverflow.com/help/licensing) . rev 2026.3.20.41360
Readable Markdown
In general you should probably have a look at the following answers: - <https://stackoverflow.com/a/13434501/604048> - <https://stackoverflow.com/a/24489602/604048> However, I'd like to address number 3. 1. You should use `numpy` to check for `NaN`s, not use the equals operator. `np.isnan(...)`. 2. You shouldn't use a `while` loop. It's very unpythonic. 3. Your if statement shouldn't have a parenthesis. 4. The indentation of `while` part should make this script fail. 5. Remember you're working with rows. You shouldn't get the ambiguous truth value error if your code was implemented the way you showed. Maybe you wrote something like this: ``` i = 0 while(i < len(y)): if np.isnan(y[i]): y[i] = 0 i = i + 1 ``` This produces: > ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() What is happening here is that the if statement gets a list of bools, and doesn't know what to do with it. Is it true when all of them are true? Or, is it true when any of them are true? You could resolve it as follows: ``` i = 0 while(i < len(y)): if np.isnan(y[i]).any(): y[i] = 0 i = i + 1 ``` Or more pythonic: ``` for i in range(0, len(y)): if np.isnan(y[i]).any(): y[i] = 0 ``` Also be careful about the zero assignment. I don't know how pandas data frames are implemented, but typically with normal collection types you'd be replacing a row with the number zero (0). If we weren't working with special pandas data frames, and it was simply lists I'd go for: ``` y = [row for row in y if not np.isnan(row).any()] ``` ... if you wanted to delete a row with any `NaN` values. Or, if you simply want to set any element which is `NaN` to zero: ``` y = [[0 if np.isnan(elm) else elm for elm in row] for row in y] ``` With test data: ``` import numpy as np y = [[0,0],[1,1],[2,float('nan')],[3,3]] print(y) x = [row for row in y if not np.isnan(row).any()] print(x) z = [[0 if np.isnan(elm) else elm for elm in row] for row in y] print(z) ``` This produces the following output: ``` [[0, 0], [1, 1], [2, nan], [3, 3]] [[0, 0], [1, 1], [3, 3]] [[0, 0], [1, 1], [2, 0], [3, 3]] ```
Shard18 (laksa)
Root Hash8045678284012640218
Unparsed URLcom,stackexchange!quant,/questions/31094/removing-nan-values-in-python-quantopian s443