pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.7k stars 17.92k forks source link

ValueError: The truth value of an array with more than one element is ambiguous. #20622

Closed perl-girl closed 6 years ago

perl-girl commented 6 years ago
            if (self.index_label and
                    isinstance(self.index_label, (list, tuple, np.ndarray,
                                                  Index))):

Problem description

self.index_label can be an array, list, Index or tuple, but boolean values of arrays are ambiguous. Should be changed to

        if (self.index_label is not None and
                isinstance(self.index_label, (list, tuple, np.ndarray,
                                              Index))):

Expected Output

Formatted excel cell

Output of pandas/io/formats/excel.get_formatted_cells

in get_formatted_cells yield cell in _format_hierarchical_rows if (self.index_label and isinstance(self.index_label, (list, tuple, np.ndarray, Index))): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
TomAugspurger commented 6 years ago

Can you provide a copy-pastable code-snippet that produces an error?

What version of pandas?

perl-girl commented 6 years ago

Name: pandas Version: 0.22.0

perl-girl commented 6 years ago

dataFrame.index.values is a tuple

from pandas import DataFrame, MultiIndex, ExcelWriter

    def DF2Excel(self, dataFrame, filename='output.xlsx'):
        writer = ExcelWriter(filename)
        names = dataFrame.index.values
        dataFrame.to_excel(writer,index_label=names,merge_cells=False)
        writer.save()
TomAugspurger commented 6 years ago

See http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports for tips on writing a reproducible example. Let us know if you can provide one.