Check and cast multiindex types












1















How can I check a Pandas' Multiindex type for each of its levels?
I'm trying to access a specific level in order to check whether its type is what I want it to be, and if not, cast it to an int type.



I've tried df.index.info() with no success. I've also checked the methods and attributes described in the API Reference, though I don't see any mention to it.










share|improve this question



























    1















    How can I check a Pandas' Multiindex type for each of its levels?
    I'm trying to access a specific level in order to check whether its type is what I want it to be, and if not, cast it to an int type.



    I've tried df.index.info() with no success. I've also checked the methods and attributes described in the API Reference, though I don't see any mention to it.










    share|improve this question

























      1












      1








      1








      How can I check a Pandas' Multiindex type for each of its levels?
      I'm trying to access a specific level in order to check whether its type is what I want it to be, and if not, cast it to an int type.



      I've tried df.index.info() with no success. I've also checked the methods and attributes described in the API Reference, though I don't see any mention to it.










      share|improve this question














      How can I check a Pandas' Multiindex type for each of its levels?
      I'm trying to access a specific level in order to check whether its type is what I want it to be, and if not, cast it to an int type.



      I've tried df.index.info() with no success. I've also checked the methods and attributes described in the API Reference, though I don't see any mention to it.







      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 14:31









      jimijazzjimijazz

      954822




      954822
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Setup



          idx = pd.MultiIndex.from_product([range(2), [*'XY']], names=['lvl0', 'lvl1'])
          df = pd.DataFrame(1, idx, idx)
          df

          lvl0 0 1
          lvl1 X Y X Y
          lvl0 lvl1
          0 X 1 1 1 1
          Y 1 1 1 1
          1 X 1 1 1 1
          Y 1 1 1 1




          Basic Anatomy of a MultiIndex



          levels



          Is a frozenlist of pandas.Index objects. Each of these pandas.Index objects should contain unique values. If these level index objects are not unique, something is probably broken.



          [*map(type, df.index.levels)]

          [pandas.core.indexes.numeric.Int64Index, pandas.core.indexes.base.Index]


          you can get at their dtype



          [l.dtype for l in df.index.levels]

          [dtype('int64'), dtype('O')]




          labels



          This is a frozenlist of arrays. There is a one label array for each level index. The corresponding label array contains reference to which level values are being displayed.



          [*map(type, df.index.labels)]

          [pandas.core.indexes.frozen.FrozenNDArray,
          pandas.core.indexes.frozen.FrozenNDArray]




          print(*df.index.labels, sep='n')

          FrozenNDArray([0, 0, 1, 1], dtype='int8')
          FrozenNDArray([0, 1, 0, 1], dtype='int8')




          get_level_values



          You can access the values in an index with get_level_values



          df.index.get_level_values(1)

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')


          Which would be the same as slicing the level object with the label object



          df.index.levels[1][df.index.labels[1]]

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')





          share|improve this answer



















          • 1





            happy thanksgiving:-)

            – Wen-Ben
            Nov 21 '18 at 14:57











          • You too! And thanks (-:

            – piRSquared
            Nov 21 '18 at 14:58



















          1














          Using get_level_values



          df.index.get_level_values(0).dtype
          Out[19]: dtype('int64')





          share|improve this answer
























          • this is a good answer, but for the sake of completeness I've marked the other one as accepted. Thanks anyways!

            – jimijazz
            Nov 22 '18 at 13:18











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414342%2fcheck-and-cast-multiindex-types%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Setup



          idx = pd.MultiIndex.from_product([range(2), [*'XY']], names=['lvl0', 'lvl1'])
          df = pd.DataFrame(1, idx, idx)
          df

          lvl0 0 1
          lvl1 X Y X Y
          lvl0 lvl1
          0 X 1 1 1 1
          Y 1 1 1 1
          1 X 1 1 1 1
          Y 1 1 1 1




          Basic Anatomy of a MultiIndex



          levels



          Is a frozenlist of pandas.Index objects. Each of these pandas.Index objects should contain unique values. If these level index objects are not unique, something is probably broken.



          [*map(type, df.index.levels)]

          [pandas.core.indexes.numeric.Int64Index, pandas.core.indexes.base.Index]


          you can get at their dtype



          [l.dtype for l in df.index.levels]

          [dtype('int64'), dtype('O')]




          labels



          This is a frozenlist of arrays. There is a one label array for each level index. The corresponding label array contains reference to which level values are being displayed.



          [*map(type, df.index.labels)]

          [pandas.core.indexes.frozen.FrozenNDArray,
          pandas.core.indexes.frozen.FrozenNDArray]




          print(*df.index.labels, sep='n')

          FrozenNDArray([0, 0, 1, 1], dtype='int8')
          FrozenNDArray([0, 1, 0, 1], dtype='int8')




          get_level_values



          You can access the values in an index with get_level_values



          df.index.get_level_values(1)

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')


          Which would be the same as slicing the level object with the label object



          df.index.levels[1][df.index.labels[1]]

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')





          share|improve this answer



















          • 1





            happy thanksgiving:-)

            – Wen-Ben
            Nov 21 '18 at 14:57











          • You too! And thanks (-:

            – piRSquared
            Nov 21 '18 at 14:58
















          1














          Setup



          idx = pd.MultiIndex.from_product([range(2), [*'XY']], names=['lvl0', 'lvl1'])
          df = pd.DataFrame(1, idx, idx)
          df

          lvl0 0 1
          lvl1 X Y X Y
          lvl0 lvl1
          0 X 1 1 1 1
          Y 1 1 1 1
          1 X 1 1 1 1
          Y 1 1 1 1




          Basic Anatomy of a MultiIndex



          levels



          Is a frozenlist of pandas.Index objects. Each of these pandas.Index objects should contain unique values. If these level index objects are not unique, something is probably broken.



          [*map(type, df.index.levels)]

          [pandas.core.indexes.numeric.Int64Index, pandas.core.indexes.base.Index]


          you can get at their dtype



          [l.dtype for l in df.index.levels]

          [dtype('int64'), dtype('O')]




          labels



          This is a frozenlist of arrays. There is a one label array for each level index. The corresponding label array contains reference to which level values are being displayed.



          [*map(type, df.index.labels)]

          [pandas.core.indexes.frozen.FrozenNDArray,
          pandas.core.indexes.frozen.FrozenNDArray]




          print(*df.index.labels, sep='n')

          FrozenNDArray([0, 0, 1, 1], dtype='int8')
          FrozenNDArray([0, 1, 0, 1], dtype='int8')




          get_level_values



          You can access the values in an index with get_level_values



          df.index.get_level_values(1)

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')


          Which would be the same as slicing the level object with the label object



          df.index.levels[1][df.index.labels[1]]

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')





          share|improve this answer



















          • 1





            happy thanksgiving:-)

            – Wen-Ben
            Nov 21 '18 at 14:57











          • You too! And thanks (-:

            – piRSquared
            Nov 21 '18 at 14:58














          1












          1








          1







          Setup



          idx = pd.MultiIndex.from_product([range(2), [*'XY']], names=['lvl0', 'lvl1'])
          df = pd.DataFrame(1, idx, idx)
          df

          lvl0 0 1
          lvl1 X Y X Y
          lvl0 lvl1
          0 X 1 1 1 1
          Y 1 1 1 1
          1 X 1 1 1 1
          Y 1 1 1 1




          Basic Anatomy of a MultiIndex



          levels



          Is a frozenlist of pandas.Index objects. Each of these pandas.Index objects should contain unique values. If these level index objects are not unique, something is probably broken.



          [*map(type, df.index.levels)]

          [pandas.core.indexes.numeric.Int64Index, pandas.core.indexes.base.Index]


          you can get at their dtype



          [l.dtype for l in df.index.levels]

          [dtype('int64'), dtype('O')]




          labels



          This is a frozenlist of arrays. There is a one label array for each level index. The corresponding label array contains reference to which level values are being displayed.



          [*map(type, df.index.labels)]

          [pandas.core.indexes.frozen.FrozenNDArray,
          pandas.core.indexes.frozen.FrozenNDArray]




          print(*df.index.labels, sep='n')

          FrozenNDArray([0, 0, 1, 1], dtype='int8')
          FrozenNDArray([0, 1, 0, 1], dtype='int8')




          get_level_values



          You can access the values in an index with get_level_values



          df.index.get_level_values(1)

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')


          Which would be the same as slicing the level object with the label object



          df.index.levels[1][df.index.labels[1]]

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')





          share|improve this answer













          Setup



          idx = pd.MultiIndex.from_product([range(2), [*'XY']], names=['lvl0', 'lvl1'])
          df = pd.DataFrame(1, idx, idx)
          df

          lvl0 0 1
          lvl1 X Y X Y
          lvl0 lvl1
          0 X 1 1 1 1
          Y 1 1 1 1
          1 X 1 1 1 1
          Y 1 1 1 1




          Basic Anatomy of a MultiIndex



          levels



          Is a frozenlist of pandas.Index objects. Each of these pandas.Index objects should contain unique values. If these level index objects are not unique, something is probably broken.



          [*map(type, df.index.levels)]

          [pandas.core.indexes.numeric.Int64Index, pandas.core.indexes.base.Index]


          you can get at their dtype



          [l.dtype for l in df.index.levels]

          [dtype('int64'), dtype('O')]




          labels



          This is a frozenlist of arrays. There is a one label array for each level index. The corresponding label array contains reference to which level values are being displayed.



          [*map(type, df.index.labels)]

          [pandas.core.indexes.frozen.FrozenNDArray,
          pandas.core.indexes.frozen.FrozenNDArray]




          print(*df.index.labels, sep='n')

          FrozenNDArray([0, 0, 1, 1], dtype='int8')
          FrozenNDArray([0, 1, 0, 1], dtype='int8')




          get_level_values



          You can access the values in an index with get_level_values



          df.index.get_level_values(1)

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')


          Which would be the same as slicing the level object with the label object



          df.index.levels[1][df.index.labels[1]]

          Index(['X', 'Y', 'X', 'Y'], dtype='object', name='lvl1')






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:52









          piRSquaredpiRSquared

          155k22150294




          155k22150294








          • 1





            happy thanksgiving:-)

            – Wen-Ben
            Nov 21 '18 at 14:57











          • You too! And thanks (-:

            – piRSquared
            Nov 21 '18 at 14:58














          • 1





            happy thanksgiving:-)

            – Wen-Ben
            Nov 21 '18 at 14:57











          • You too! And thanks (-:

            – piRSquared
            Nov 21 '18 at 14:58








          1




          1





          happy thanksgiving:-)

          – Wen-Ben
          Nov 21 '18 at 14:57





          happy thanksgiving:-)

          – Wen-Ben
          Nov 21 '18 at 14:57













          You too! And thanks (-:

          – piRSquared
          Nov 21 '18 at 14:58





          You too! And thanks (-:

          – piRSquared
          Nov 21 '18 at 14:58













          1














          Using get_level_values



          df.index.get_level_values(0).dtype
          Out[19]: dtype('int64')





          share|improve this answer
























          • this is a good answer, but for the sake of completeness I've marked the other one as accepted. Thanks anyways!

            – jimijazz
            Nov 22 '18 at 13:18
















          1














          Using get_level_values



          df.index.get_level_values(0).dtype
          Out[19]: dtype('int64')





          share|improve this answer
























          • this is a good answer, but for the sake of completeness I've marked the other one as accepted. Thanks anyways!

            – jimijazz
            Nov 22 '18 at 13:18














          1












          1








          1







          Using get_level_values



          df.index.get_level_values(0).dtype
          Out[19]: dtype('int64')





          share|improve this answer













          Using get_level_values



          df.index.get_level_values(0).dtype
          Out[19]: dtype('int64')






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:49









          Wen-BenWen-Ben

          110k83266




          110k83266













          • this is a good answer, but for the sake of completeness I've marked the other one as accepted. Thanks anyways!

            – jimijazz
            Nov 22 '18 at 13:18



















          • this is a good answer, but for the sake of completeness I've marked the other one as accepted. Thanks anyways!

            – jimijazz
            Nov 22 '18 at 13:18

















          this is a good answer, but for the sake of completeness I've marked the other one as accepted. Thanks anyways!

          – jimijazz
          Nov 22 '18 at 13:18





          this is a good answer, but for the sake of completeness I've marked the other one as accepted. Thanks anyways!

          – jimijazz
          Nov 22 '18 at 13:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414342%2fcheck-and-cast-multiindex-types%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

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory