AttributeError with dataframe_to_rows on a dataframe with string columns
Supplying a DataFrame to dataframe_to_rows
where a column has the string
dtype results in an error.
This scikit-learn bug with a similar message may be a hint.
Example Code
import pandas as pd
import openpyxl
from openpyxl.utils.dataframe import dataframe_to_rows
pd.__version__
'1.1.3'
openpyxl.__version__
'3.0.5'
numbers = pd.Series([1, 2, 3])
letters = pd.Series(['a', 'b', 'c'])
letters_as_string = pd.Series(['a', 'b', 'c'], dtype="string")
df = pd.DataFrame({"numbers": numbers, "letters": letters, "letters_as_string": letters_as_string})
wb = openpyxl.Workbook()
ws = wb.active
for r in dataframe_to_rows(df[["numbers", "letters"]], index=False, header=True):
ws.append(r)
for value in ws.values:
print(value)
('numbers', 'letters')
(1, 'a')
(2, 'b')
(3, 'c')
for r in dataframe_to_rows(df, index=False, header=True):
ws.append(r)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-1aaaaf90493b> in <module>
----> 1 for r in dataframe_to_rows(df, index=False, header=True):
2 ws.append(r)
~/miniconda3/lib/python3.8/site-packages/openpyxl/utils/dataframe.py in dataframe_to_rows(df, index, header)
25 values = values.reshape(b.shape)
26
---> 27 result = values.tolist()
28
29 for col_loc, col in zip(b.mgr_locs, result):
AttributeError: 'StringArray' object has no attribute 'tolist'
Edited by Roger Filmyer