Indexing empty array, Numba vs. Numpy











up vote
3
down vote

favorite












I was experimenting with the behavior of Numba vs Numpy for array indexing, and I came across something which I do not quite understand; so I hoped someone can point me in the right direction for what is probably a very simple question. Below are two functions, both of which create an empty array using the np.arange command. I then "append" (experimenting with a variety of methods to see how both Numba and Numpy perform/break) to the array using an index of 0, example[0] = 1.



The Numba function with jit runs without error, but the Numpy example gives the error:



IndexError: index 0 is out of bounds for axis 0 with size 0


The Numpy error makes sense, but I am unsure as to why Numba with jit enabled allows the operation without error.



import numba as nb
import numpy as np

@nb.jit()
def funcnumba():
'''
Add item to position 0 using Numba
'''
example = np.arange(0)
example[0] = 1
return example

def funcnumpy():
'''
Add item to position 0 using Numpy. This produces an error which makes sense
'''
example = np.arange(0)
example[0] = 1
return example

print(funcnumba())

print(funcnumpy())









share|improve this question


























    up vote
    3
    down vote

    favorite












    I was experimenting with the behavior of Numba vs Numpy for array indexing, and I came across something which I do not quite understand; so I hoped someone can point me in the right direction for what is probably a very simple question. Below are two functions, both of which create an empty array using the np.arange command. I then "append" (experimenting with a variety of methods to see how both Numba and Numpy perform/break) to the array using an index of 0, example[0] = 1.



    The Numba function with jit runs without error, but the Numpy example gives the error:



    IndexError: index 0 is out of bounds for axis 0 with size 0


    The Numpy error makes sense, but I am unsure as to why Numba with jit enabled allows the operation without error.



    import numba as nb
    import numpy as np

    @nb.jit()
    def funcnumba():
    '''
    Add item to position 0 using Numba
    '''
    example = np.arange(0)
    example[0] = 1
    return example

    def funcnumpy():
    '''
    Add item to position 0 using Numpy. This produces an error which makes sense
    '''
    example = np.arange(0)
    example[0] = 1
    return example

    print(funcnumba())

    print(funcnumpy())









    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I was experimenting with the behavior of Numba vs Numpy for array indexing, and I came across something which I do not quite understand; so I hoped someone can point me in the right direction for what is probably a very simple question. Below are two functions, both of which create an empty array using the np.arange command. I then "append" (experimenting with a variety of methods to see how both Numba and Numpy perform/break) to the array using an index of 0, example[0] = 1.



      The Numba function with jit runs without error, but the Numpy example gives the error:



      IndexError: index 0 is out of bounds for axis 0 with size 0


      The Numpy error makes sense, but I am unsure as to why Numba with jit enabled allows the operation without error.



      import numba as nb
      import numpy as np

      @nb.jit()
      def funcnumba():
      '''
      Add item to position 0 using Numba
      '''
      example = np.arange(0)
      example[0] = 1
      return example

      def funcnumpy():
      '''
      Add item to position 0 using Numpy. This produces an error which makes sense
      '''
      example = np.arange(0)
      example[0] = 1
      return example

      print(funcnumba())

      print(funcnumpy())









      share|improve this question













      I was experimenting with the behavior of Numba vs Numpy for array indexing, and I came across something which I do not quite understand; so I hoped someone can point me in the right direction for what is probably a very simple question. Below are two functions, both of which create an empty array using the np.arange command. I then "append" (experimenting with a variety of methods to see how both Numba and Numpy perform/break) to the array using an index of 0, example[0] = 1.



      The Numba function with jit runs without error, but the Numpy example gives the error:



      IndexError: index 0 is out of bounds for axis 0 with size 0


      The Numpy error makes sense, but I am unsure as to why Numba with jit enabled allows the operation without error.



      import numba as nb
      import numpy as np

      @nb.jit()
      def funcnumba():
      '''
      Add item to position 0 using Numba
      '''
      example = np.arange(0)
      example[0] = 1
      return example

      def funcnumpy():
      '''
      Add item to position 0 using Numpy. This produces an error which makes sense
      '''
      example = np.arange(0)
      example[0] = 1
      return example

      print(funcnumba())

      print(funcnumpy())






      python numpy numba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 14:04









      Yeti

      145112




      145112
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          See the Numba documentation on arrays:




          Currently there are no bounds checking for array indexing and slicing (...)




          That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.






          share|improve this answer





















          • I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
            – roganjosh
            Nov 19 at 14:19










          • In fact, np.arange(0) seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
            – roganjosh
            Nov 19 at 14:22










          • @roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking). np.arange(0) is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
            – jdehesa
            Nov 19 at 14:29












          • Oops, that was a bad misread on my part, sorry!
            – roganjosh
            Nov 19 at 14:31











          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',
          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%2f53376356%2findexing-empty-array-numba-vs-numpy%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








          up vote
          4
          down vote



          accepted










          See the Numba documentation on arrays:




          Currently there are no bounds checking for array indexing and slicing (...)




          That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.






          share|improve this answer





















          • I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
            – roganjosh
            Nov 19 at 14:19










          • In fact, np.arange(0) seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
            – roganjosh
            Nov 19 at 14:22










          • @roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking). np.arange(0) is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
            – jdehesa
            Nov 19 at 14:29












          • Oops, that was a bad misread on my part, sorry!
            – roganjosh
            Nov 19 at 14:31















          up vote
          4
          down vote



          accepted










          See the Numba documentation on arrays:




          Currently there are no bounds checking for array indexing and slicing (...)




          That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.






          share|improve this answer





















          • I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
            – roganjosh
            Nov 19 at 14:19










          • In fact, np.arange(0) seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
            – roganjosh
            Nov 19 at 14:22










          • @roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking). np.arange(0) is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
            – jdehesa
            Nov 19 at 14:29












          • Oops, that was a bad misread on my part, sorry!
            – roganjosh
            Nov 19 at 14:31













          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          See the Numba documentation on arrays:




          Currently there are no bounds checking for array indexing and slicing (...)




          That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.






          share|improve this answer












          See the Numba documentation on arrays:




          Currently there are no bounds checking for array indexing and slicing (...)




          That means that you will be writing out of the bounds of the array in this case. Since it is just one element you may be lucky and get away with it, but you can also crash your program or, even worse, silently overwrite some other value. See issue #730 for a discussion about it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 14:17









          jdehesa

          21.9k43150




          21.9k43150












          • I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
            – roganjosh
            Nov 19 at 14:19










          • In fact, np.arange(0) seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
            – roganjosh
            Nov 19 at 14:22










          • @roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking). np.arange(0) is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
            – jdehesa
            Nov 19 at 14:29












          • Oops, that was a bad misread on my part, sorry!
            – roganjosh
            Nov 19 at 14:31


















          • I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
            – roganjosh
            Nov 19 at 14:19










          • In fact, np.arange(0) seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
            – roganjosh
            Nov 19 at 14:22










          • @roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking). np.arange(0) is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
            – jdehesa
            Nov 19 at 14:29












          • Oops, that was a bad misread on my part, sorry!
            – roganjosh
            Nov 19 at 14:31
















          I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
          – roganjosh
          Nov 19 at 14:19




          I'm confused, why does it throw the error when there are no bounds checks? Isn't this more to do with numpy treating single item arrays as scalars?
          – roganjosh
          Nov 19 at 14:19












          In fact, np.arange(0) seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
          – roganjosh
          Nov 19 at 14:22




          In fact, np.arange(0) seems to me to be inviting wonky behaviour tbh. Does it have a purpose?
          – roganjosh
          Nov 19 at 14:22












          @roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking). np.arange(0) is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
          – jdehesa
          Nov 19 at 14:29






          @roganjosh It's the NumPy function the one that raises the error, not the Numba one (which makes sense, because NumPy does implement bounds checking). np.arange(0) is an empty array, not a single-item one. Wrt its utility, I'd say it's a matter of consistence. I have probably used it when I needed something to work whether the result should be empty or not.
          – jdehesa
          Nov 19 at 14:29














          Oops, that was a bad misread on my part, sorry!
          – roganjosh
          Nov 19 at 14:31




          Oops, that was a bad misread on my part, sorry!
          – roganjosh
          Nov 19 at 14:31


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53376356%2findexing-empty-array-numba-vs-numpy%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy