{"id":13256,"date":"2021-06-08T17:54:22","date_gmt":"2021-06-08T12:24:22","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=13256"},"modified":"2026-07-13T12:34:49","modified_gmt":"2026-07-13T07:04:49","slug":"numpy-piecewise","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/numpy-piecewise\/","title":{"rendered":"NumPy piecewise: Conditional Array Values"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Quick_Answer\" >Quick Answer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Apply_Constants_By_Condition\" >Apply Constants By Condition<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Use_Functions_In_funclist\" >Use Functions In funclist<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Use_A_Default_Value\" >Use A Default Value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Build_A_Piecewise_Formula\" >Build A Piecewise Formula<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Combine_Several_Ranges\" >Combine Several Ranges<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Compare_piecewise_With_where\" >Compare piecewise With where<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Common_Mistakes\" >Common Mistakes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Apply_Functions_and_Constants_by_Condition\" >Apply Functions and Constants by Condition<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Use_an_Explicit_Default_for_Uncovered_Values\" >Use an Explicit Default for Uncovered Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#What_does_nppiecewise_do\" >What does np.piecewise do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#What_shape_should_NumPy_piecewise_conditions_have\" >What shape should NumPy piecewise conditions have?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#How_do_I_set_a_default_value_with_nppiecewise\" >How do I set a default value with np.piecewise?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/#When_should_I_use_npwhere_or_npselect_instead\" >When should I use np.where or np.select instead?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Quick_Answer\"><\/span>Quick Answer<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Call <code>np.piecewise(x, condlist, funclist)<\/code> to apply different constants or functions to elements selected by boolean conditions. Each condition should match <code>x<\/code> in shape. If <code>funclist<\/code> has one extra item, that item supplies the default for elements not covered by any condition.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise.png\" alt=\"NumPy piecewise condition arrays functions and default output\" width=\"1600\" height=\"1066\" loading=\"lazy\" decoding=\"async\"><figcaption>numpy.piecewise applies each function where its condition is true and can use an extra function as the uncovered-value default.<\/figcaption><\/figure>\n<p><code>np.piecewise()<\/code> applies different values or functions to different parts of an array. It is useful when each condition has its own rule.<\/p>\n<p>The official <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.piecewise.html\">NumPy piecewise documentation<\/a> explains <code>condlist<\/code> and <code>funclist<\/code>. The <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.where.html\">NumPy where documentation<\/a> is useful for simpler two-way choices.<\/p>\n<p>Use <code>piecewise()<\/code> when conditions are easier to read as separate rules. For one simple true\/false choice, <code>np.where()<\/code> is often shorter.<\/p>\n<p>The function is best for formulas that are naturally described as ranges. Each condition names a slice of the input, and each matching slice receives its own output rule.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Apply_Constants_By_Condition\"><\/span>Apply Constants By Condition<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The condition list selects positions in the input array, and the function list supplies the output for those positions. piecewise selects formulas from conditions, while <a href=\"https:\/\/www.pythonpool.com\/numpy-choose\/\">NumPy choose() Function Examples Guide<\/a> selects values from indexed choice arrays.<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nx = np.array([-2, -1, 0, 1, 2])\n\nresult = np.piecewise(\n    x,\n    [x &lt; 0, x == 0, x &gt; 0],\n    [-1, 0, 1],\n)\n\nprint(result)\n<\/code><\/pre>\n<p>This maps negative values to <code>-1<\/code>, zero to <code>0<\/code>, and positive values to <code>1<\/code>. Each condition produces one part of the final output. For a standard step discontinuity without writing condition branches manually, <a href=\"https:\/\/www.pythonpool.com\/numpy-heaviside\/\">NumPy heaviside Guide for Step Functions<\/a> provides NumPy&#8217;s Heaviside definition and the value at zero.<\/p>\n<p>The output shape matches the input shape, which makes it easy to compare the result against the original array.<\/p>\n<p>Keep the condition order readable. When possible, write conditions from low to high, left to right, or from most specific to most general so the rule list has an obvious flow.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/piecewise-conditions-b169.png\" alt=\"Python Pool infographic showing an input array, Boolean conditions, branches, and NumPy piecewise\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Conditions: An input array, Boolean conditions, branches, and NumPy piecewise.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_Functions_In_funclist\"><\/span>Use Functions In funclist<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Items in <code>funclist<\/code> can be callables. NumPy passes the matching values to the function.<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nx = np.array([-3, -2, -1, 0, 1, 2, 3])\n\nresult = np.piecewise(\n    x,\n    [x &lt; 0, x &gt;= 0],\n    [lambda values: values * values, lambda values: values + 10],\n)\n\nprint(result)\n<\/code><\/pre>\n<p>The first function squares negative values. The second function adds 10 to zero and positive values.<\/p>\n<p>Functions should be written to handle arrays, not just one scalar value, because each branch receives all matching values at once.<\/p>\n<p>This vectorized call style is important for performance. The function runs once per branch with a slice of data, rather than once for every individual item.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_A_Default_Value\"><\/span>Use A Default Value<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If <code>funclist<\/code> has one more item than <code>condlist<\/code>, the final item is used as the default for values that match none of the conditions.<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nx = np.array([5, 15, 25, 35])\n\nresult = np.piecewise(\n    x,\n    [x &lt; 10, x &gt; 30],\n    [\"low\", \"high\", \"middle\"],\n)\n\nprint(result)\n<\/code><\/pre>\n<p>Values below 10 become <code>low<\/code>, values above 30 become <code>high<\/code>, and the rest use the default <code>middle<\/code>.<\/p>\n<p>Without a default, unmatched positions receive zero-like values based on the output dtype. An explicit default is usually clearer.<\/p>\n<p>Defaults also make intent visible in reviews. A reader can immediately see what happens outside the named ranges instead of guessing whether zero was intentional.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/piecewise-values-b169.png\" alt=\"Python Pool infographic mapping each condition to a scalar or function and selecting output values\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Branch values: Each condition to a scalar or function and selecting output values.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Build_A_Piecewise_Formula\"><\/span>Build A Piecewise Formula<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Piecewise math is a natural fit when different numeric ranges need different formulas.<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nx = np.linspace(-2, 2, 5)\n\ny = np.piecewise(\n    x,\n    [x &lt; 0, x &gt;= 0],\n    [lambda values: -values, lambda values: values ** 2],\n)\n\nprint(x)\nprint(y)\n<\/code><\/pre>\n<p>This uses absolute-value-like behavior on the negative side and a square on the nonnegative side.<\/p>\n<p>For plotting or simulation, keeping the rules inside one <code>piecewise()<\/code> call can make the formula easier to inspect.<\/p>\n<p>When the formulas are longer, define named helper functions before calling <code>piecewise()<\/code>. That keeps the rule list compact while still preserving vectorized behavior.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Combine_Several_Ranges\"><\/span>Combine Several Ranges<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Multiple conditions can describe bands or categories.<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nscores = np.array([45, 62, 74, 88, 96])\n\ngrades = np.piecewise(\n    scores,\n    [scores &lt; 60, (scores &gt;= 60) &amp; (scores &lt; 75), scores &gt;= 75],\n    [\"fail\", \"pass\", \"strong\"],\n)\n\nprint(grades)\n<\/code><\/pre>\n<p>The middle condition uses <code>&amp;<\/code> because it combines two NumPy Boolean masks. Parentheses are required around each comparison.<\/p>\n<p>When ranges are adjacent, check the boundary values carefully so every value lands in the intended category.<\/p>\n<p>It is also worth checking dtype. If every output is a string label, the result becomes a string array. If outputs are numeric, NumPy chooses a numeric dtype that can hold the branch results.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/piecewise-default-b169.png\" alt=\"Python Pool infographic comparing matched conditions, default value, overlap, order, and broadcast shape\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Default branch: Matched conditions, default value, overlap, order, and broadcast shape.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Compare_piecewise_With_where\"><\/span>Compare piecewise With where<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>np.where()<\/code> is often better for one condition and two possible outputs.<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nx = np.array([-2, -1, 0, 1, 2])\n\nwith_where = np.where(x &lt; 0, -x, x)\nwith_piecewise = np.piecewise(x, [x &lt; 0, x &gt;= 0], [lambda v: -v, lambda v: v])\n\nprint(with_where)\nprint(with_piecewise)\n<\/code><\/pre>\n<p>Both results are the same in this simple case. <code>where()<\/code> is shorter, while <code>piecewise()<\/code> scales better when there are several named conditions.<\/p>\n<p>Choose the function that keeps the rule easiest to read, not the one with the fewest characters.<\/p>\n<p>As a rough rule, use <code>where()<\/code> for binary choices and <code>piecewise()<\/code> when the reader benefits from seeing a list of named conditions.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Common_Mistakes\"><\/span>Common Mistakes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Do not mismatch condition and function counts unless the extra function is intended as the default.<\/p>\n<p>Do not forget that overlapping conditions are applied in list order. Later rules can write to positions already handled by earlier rules, so make ranges exclusive when possible.<\/p>\n<p>Do not use scalar-only functions in <code>funclist<\/code>. Branch functions receive arrays of matching values.<\/p>\n<p>The practical default is to use <code>np.where()<\/code> for one simple condition and <code>np.piecewise()<\/code> when several conditions each need their own value or formula.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/piecewise-check-b169.png\" alt=\"Python Pool infographic testing no match, overlapping masks, dtype, broadcasting, and empty arrays\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Piecewise checks: No match, overlapping masks, dtype, broadcasting, and empty arrays.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Apply_Functions_and_Constants_by_Condition\"><\/span>Apply Functions and Constants by Condition<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Each condition in <code>condlist<\/code> selects the positions where the corresponding item in <code>funclist<\/code> is used. A scalar funclist item is treated as a constant; a callable receives the selected input.<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nx = np.array([-2, -1, 0, 2, 5])\nconditions = [x &lt; 0, x == 0, x &gt; 0]\nfunctions = [-1, 0, lambda values: values * 2]\n\nresult = np.piecewise(x, conditions, functions)\nprint(result)\n<\/code><\/pre>\n<p>Make conditions mutually understandable and cover the intended domain. Overlapping conditions can make the result depend on the order in which conditions are applied, so document that choice.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_an_Explicit_Default_for_Uncovered_Values\"><\/span>Use an Explicit Default for Uncovered Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If no condition is true, the default output is zero unless an extra function or scalar is provided. Give an explicit default when zero would be misleading.<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nx = np.array([-2, 0, 3])\nresult = np.piecewise(\n    x,\n    [x &gt; 0],\n    [lambda values: values ** 2, -999],\n)\nprint(result)  # [-999, -999, 9]\n<\/code><\/pre>\n<p>For simple element-wise conditions, compare <code>np.where()<\/code> or <code>np.select()<\/code> as well. Choose the function whose control flow is easiest to verify and whose conditions have compatible shapes.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_does_nppiecewise_do\"><\/span>What does np.piecewise do?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It evaluates different constants or functions over the elements of an array selected by boolean condition arrays.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_shape_should_NumPy_piecewise_conditions_have\"><\/span>What shape should NumPy piecewise conditions have?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Each condition should have the same shape as x so every selected element maps to the intended output position.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_set_a_default_value_with_nppiecewise\"><\/span>How do I set a default value with np.piecewise?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Provide one more item in funclist than in condlist. The extra item is used where all conditions are false.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"When_should_I_use_npwhere_or_npselect_instead\"><\/span>When should I use np.where or np.select instead?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use them when nested choices or simple conditions are clearer there; piecewise is useful when each region maps to a callable or a scalar rule.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What does np.piecewise do?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"It evaluates different constants or functions over the elements of an array selected by boolean condition arrays.\"}},{\"@type\":\"Question\",\"name\":\"What shape should NumPy piecewise conditions have?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Each condition should have the same shape as x so every selected element maps to the intended output position.\"}},{\"@type\":\"Question\",\"name\":\"How do I set a default value with np.piecewise?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Provide one more item in funclist than in condlist. The extra item is used where all conditions are false.\"}},{\"@type\":\"Question\",\"name\":\"When should I use np.where or np.select instead?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use them when nested choices or simple conditions are clearer there; piecewise is useful when each region maps to a callable or a scalar rule.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Evaluate different constants or functions over NumPy array regions with piecewise, matching condition shapes and optional defaults.<\/p>\n","protected":false},"author":20,"featured_media":33755,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1495],"tags":[4184,4186,4185,4187],"class_list":["post-13256","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-numpy-piecewise-function-integration","tag-numpy-piecewise-product-matrix","tag-piecewise-add-numpy-arrays","tag-piecewise-operation-on-numpy-array","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>NumPy piecewise: Conditional Array Values<\/title>\n<meta name=\"description\" content=\"Use NumPy piecewise with condition arrays, constants, functions, defaults, and shape checks for conditional array calculations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy piecewise: Conditional Array Values\" \/>\n<meta property=\"og:description\" content=\"Evaluate different constants or functions over NumPy array regions with piecewise, matching condition shapes and optional defaults.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/numpy-piecewise\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-08T12:24:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T07:04:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise-guide-pythonpool.png\" \/>\n<meta name=\"author\" content=\"Dhruvi Vikma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise-guide-pythonpool.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dhruvi Vikma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/\"},\"author\":{\"name\":\"Dhruvi Vikma\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/bed8fc40c7b71baf7d76b1cfefd79f23\"},\"headline\":\"NumPy piecewise: Conditional Array Values\",\"datePublished\":\"2021-06-08T12:24:22+00:00\",\"dateModified\":\"2026-07-13T07:04:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/\"},\"wordCount\":1026,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-piecewise-guide-pythonpool.png\",\"keywords\":[\"numpy piecewise function integration\",\"numpy piecewise product matrix\",\"piecewise add numpy arrays\",\"piecewise operation on numpy array\"],\"articleSection\":[\"Numpy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/\",\"name\":\"NumPy piecewise: Conditional Array Values\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-piecewise-guide-pythonpool.png\",\"datePublished\":\"2021-06-08T12:24:22+00:00\",\"dateModified\":\"2026-07-13T07:04:49+00:00\",\"description\":\"Use NumPy piecewise with condition arrays, constants, functions, defaults, and shape checks for conditional array calculations.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-piecewise-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-piecewise-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"NumPy piecewise guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-piecewise\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"NumPy piecewise: Conditional Array Values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/bed8fc40c7b71baf7d76b1cfefd79f23\",\"name\":\"Dhruvi Vikma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"caption\":\"Dhruvi Vikma\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"NumPy piecewise: Conditional Array Values","description":"Use NumPy piecewise with condition arrays, constants, functions, defaults, and shape checks for conditional array calculations.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/numpy-piecewise\/","og_locale":"en_US","og_type":"article","og_title":"NumPy piecewise: Conditional Array Values","og_description":"Evaluate different constants or functions over NumPy array regions with piecewise, matching condition shapes and optional defaults.","og_url":"https:\/\/www.pythonpool.com\/numpy-piecewise\/","og_site_name":"Python Pool","article_published_time":"2021-06-08T12:24:22+00:00","article_modified_time":"2026-07-13T07:04:49+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise-guide-pythonpool.png","type":"","width":"","height":""}],"author":"Dhruvi Vikma","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise-guide-pythonpool.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Dhruvi Vikma","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/"},"author":{"name":"Dhruvi Vikma","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23"},"headline":"NumPy piecewise: Conditional Array Values","datePublished":"2021-06-08T12:24:22+00:00","dateModified":"2026-07-13T07:04:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/"},"wordCount":1026,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise-guide-pythonpool.png","keywords":["numpy piecewise function integration","numpy piecewise product matrix","piecewise add numpy arrays","piecewise operation on numpy array"],"articleSection":["Numpy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/numpy-piecewise\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/","url":"https:\/\/www.pythonpool.com\/numpy-piecewise\/","name":"NumPy piecewise: Conditional Array Values","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise-guide-pythonpool.png","datePublished":"2021-06-08T12:24:22+00:00","dateModified":"2026-07-13T07:04:49+00:00","description":"Use NumPy piecewise with condition arrays, constants, functions, defaults, and shape checks for conditional array calculations.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/numpy-piecewise\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-piecewise-guide-pythonpool.png","width":1350,"height":650,"caption":"NumPy piecewise guide"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/numpy-piecewise\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"NumPy piecewise: Conditional Array Values"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23","name":"Dhruvi Vikma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","caption":"Dhruvi Vikma"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=13256"}],"version-history":[{"count":28,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13256\/revisions"}],"predecessor-version":[{"id":41681,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13256\/revisions\/41681"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/33755"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=13256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=13256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=13256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}