votes up 5

ascending must be a bool value

Package:
pandas
github stars 30911
Exception Class:
TypeError

Raise code

if isinstance(ascending, list):
            if len(ascending) != 1:
                raise TypeError("ascending must be a list of bool values of length 1")
            ascending = ascending[0]

        if not isinstance(ascending, bool):
            raise TypeError("ascending must be a bool value")

        return self.sort_values(return_indexer=True, ascending=ascending)

    def _get_level_values(self, level) -> Index:
        """
        Return an Index of values for requested level.
 """

Ways to fix

votes up 1 votes down

Summary:

This exception is thrown when the sortlevel function is called on an instance of Index from pandas. This function has an optional parameter for ascending that must be set equal to a Boolean. If it is set equal to anything else the exception will be thrown. Therefore in order to avoid any potential problems, ensure ascending is set to True or False, or leave it unchanged because it is set to True by default.

Code to Reproduce the Error (Wrong):

from pandas.core.indexes.base import Index

lst = [1,2,3]
i = Index(lst)
i.sortlevel(0, ["f"])

Error Message:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-becd787a3ffe> in <module>()
      2 lst = [1,2,3]
      3 i = Index(lst)
----> 4 i.sortlevel(0, ["f"])

/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in sortlevel(self, level, ascending, sort_remaining)
   1765 
   1766         if not isinstance(ascending, bool):
-> 1767             raise TypeError("ascending must be a bool value")
   1768 
   1769         return self.sort_values(return_indexer=True, ascending=ascending)

TypeError: ascending must be a bool value

Working Version:

from pandas.core.indexes.base import Index

lst = [1,2,3]
i = Index(lst)
i.sortlevel(0, False)
Jul 30, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix