pandas sort_index (sometimes?) throws error : IndexError: pop index out of range











up vote
0
down vote

favorite












I have a dataframe with 3 indexes, and i am trying to sort them. Unfortunately depending on the order that I try and sort the indexes I sometimes get the following error:



Traceback (most recent call last):

File "<ipython-input-30-30a4bb212256>", line 1, in <module>
df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Generator','Year'], inplace=True)

File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreframe.py", line 3236, in sort_index
sort_remaining=sort_remaining)

File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreindexesmulti.py", line 1714, in sortlevel
primary = tuple(labels.pop(lev - i) for i, lev in enumerate(level))

File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreindexesmulti.py", line 1714, in <genexpr>
primary = tuple(labels.pop(lev - i) for i, lev in enumerate(level))

IndexError: pop index out of range


Looking at my dataframe, you can see the following indexes:



df_srmc_lrmc.index.names
Out[32]: FrozenList(['Year', 'Generator', 'Station_Type'])


The following sort commands work:



df_srmc_lrmc.sort_index(axis='index', level=['Generator','Year','Station_Type'], inplace=True)
df_srmc_lrmc.sort_index(axis='index', level=['Year','Station_Type', 'Generator'], inplace=True)
df_srmc_lrmc.sort_index(axis='index', level=['Year','Generator','Station_Type'], inplace=True)
df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Year','Generator'], inplace=True)


But the following don't, they throw the error mentioned above:



df_srmc_lrmc.sort_index(axis='index', level=['Generator','Station_Type','Year'], inplace=True)
df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Generator','Year'], inplace=True)


As I am sure you have guessed, I am wanting one of the sort orders that doesn't work :(



My pandas version info:



pd.__version__
Out[34]: '0.20.3'


Can someone tell me what is going on, or what I am doing wrong? I am quite new to Python so the more handholding the better.



Thanks










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a dataframe with 3 indexes, and i am trying to sort them. Unfortunately depending on the order that I try and sort the indexes I sometimes get the following error:



    Traceback (most recent call last):

    File "<ipython-input-30-30a4bb212256>", line 1, in <module>
    df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Generator','Year'], inplace=True)

    File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreframe.py", line 3236, in sort_index
    sort_remaining=sort_remaining)

    File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreindexesmulti.py", line 1714, in sortlevel
    primary = tuple(labels.pop(lev - i) for i, lev in enumerate(level))

    File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreindexesmulti.py", line 1714, in <genexpr>
    primary = tuple(labels.pop(lev - i) for i, lev in enumerate(level))

    IndexError: pop index out of range


    Looking at my dataframe, you can see the following indexes:



    df_srmc_lrmc.index.names
    Out[32]: FrozenList(['Year', 'Generator', 'Station_Type'])


    The following sort commands work:



    df_srmc_lrmc.sort_index(axis='index', level=['Generator','Year','Station_Type'], inplace=True)
    df_srmc_lrmc.sort_index(axis='index', level=['Year','Station_Type', 'Generator'], inplace=True)
    df_srmc_lrmc.sort_index(axis='index', level=['Year','Generator','Station_Type'], inplace=True)
    df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Year','Generator'], inplace=True)


    But the following don't, they throw the error mentioned above:



    df_srmc_lrmc.sort_index(axis='index', level=['Generator','Station_Type','Year'], inplace=True)
    df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Generator','Year'], inplace=True)


    As I am sure you have guessed, I am wanting one of the sort orders that doesn't work :(



    My pandas version info:



    pd.__version__
    Out[34]: '0.20.3'


    Can someone tell me what is going on, or what I am doing wrong? I am quite new to Python so the more handholding the better.



    Thanks










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a dataframe with 3 indexes, and i am trying to sort them. Unfortunately depending on the order that I try and sort the indexes I sometimes get the following error:



      Traceback (most recent call last):

      File "<ipython-input-30-30a4bb212256>", line 1, in <module>
      df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Generator','Year'], inplace=True)

      File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreframe.py", line 3236, in sort_index
      sort_remaining=sort_remaining)

      File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreindexesmulti.py", line 1714, in sortlevel
      primary = tuple(labels.pop(lev - i) for i, lev in enumerate(level))

      File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreindexesmulti.py", line 1714, in <genexpr>
      primary = tuple(labels.pop(lev - i) for i, lev in enumerate(level))

      IndexError: pop index out of range


      Looking at my dataframe, you can see the following indexes:



      df_srmc_lrmc.index.names
      Out[32]: FrozenList(['Year', 'Generator', 'Station_Type'])


      The following sort commands work:



      df_srmc_lrmc.sort_index(axis='index', level=['Generator','Year','Station_Type'], inplace=True)
      df_srmc_lrmc.sort_index(axis='index', level=['Year','Station_Type', 'Generator'], inplace=True)
      df_srmc_lrmc.sort_index(axis='index', level=['Year','Generator','Station_Type'], inplace=True)
      df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Year','Generator'], inplace=True)


      But the following don't, they throw the error mentioned above:



      df_srmc_lrmc.sort_index(axis='index', level=['Generator','Station_Type','Year'], inplace=True)
      df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Generator','Year'], inplace=True)


      As I am sure you have guessed, I am wanting one of the sort orders that doesn't work :(



      My pandas version info:



      pd.__version__
      Out[34]: '0.20.3'


      Can someone tell me what is going on, or what I am doing wrong? I am quite new to Python so the more handholding the better.



      Thanks










      share|improve this question













      I have a dataframe with 3 indexes, and i am trying to sort them. Unfortunately depending on the order that I try and sort the indexes I sometimes get the following error:



      Traceback (most recent call last):

      File "<ipython-input-30-30a4bb212256>", line 1, in <module>
      df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Generator','Year'], inplace=True)

      File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreframe.py", line 3236, in sort_index
      sort_remaining=sort_remaining)

      File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreindexesmulti.py", line 1714, in sortlevel
      primary = tuple(labels.pop(lev - i) for i, lev in enumerate(level))

      File "E:Python35WinPython-64bit-3.5.4.0Qt5python-3.5.4.amd64libsite-packagespandascoreindexesmulti.py", line 1714, in <genexpr>
      primary = tuple(labels.pop(lev - i) for i, lev in enumerate(level))

      IndexError: pop index out of range


      Looking at my dataframe, you can see the following indexes:



      df_srmc_lrmc.index.names
      Out[32]: FrozenList(['Year', 'Generator', 'Station_Type'])


      The following sort commands work:



      df_srmc_lrmc.sort_index(axis='index', level=['Generator','Year','Station_Type'], inplace=True)
      df_srmc_lrmc.sort_index(axis='index', level=['Year','Station_Type', 'Generator'], inplace=True)
      df_srmc_lrmc.sort_index(axis='index', level=['Year','Generator','Station_Type'], inplace=True)
      df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Year','Generator'], inplace=True)


      But the following don't, they throw the error mentioned above:



      df_srmc_lrmc.sort_index(axis='index', level=['Generator','Station_Type','Year'], inplace=True)
      df_srmc_lrmc.sort_index(axis='index', level=['Station_Type', 'Generator','Year'], inplace=True)


      As I am sure you have guessed, I am wanting one of the sort orders that doesn't work :(



      My pandas version info:



      pd.__version__
      Out[34]: '0.20.3'


      Can someone tell me what is going on, or what I am doing wrong? I am quite new to Python so the more handholding the better.



      Thanks







      python pandas dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 22:02









      Andrewfreestuff

      337




      337





























          active

          oldest

          votes











          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%2f53421081%2fpandas-sort-index-sometimes-throws-error-indexerror-pop-index-out-of-range%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f53421081%2fpandas-sort-index-sometimes-throws-error-indexerror-pop-index-out-of-range%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

          Sphinx de Gizeh

          Dijon

          Guerrita