问题
I am trying to programmatically create a chart in an xlsx file using xlsxwriter and pandas data. Writing cells is succeeding fine, however when I try to define a Data Series for a Scatter chart I get a TypeError 'buffer size mismatch' and I'm fairly certain my defined range is acceptable.
Example code:
headers = DataFrame([[-0.398,2],[-0.201,2],[-0.001,20]],columns=['Bias','Sensitivity'])
dfs = [DataFrame([[3998.28253,2.056],[3997.31816,1.978],[3996.35379,1.932],[3995.38942,1.746],[3994.42504,1.478]]),
DataFrame([[3998.28253,2.056],[3997.31816,1.978],[3996.35379,1.932],[3995.38942,1.746],[3994.42504,1.478]]),
DataFrame([[3998.28253,2.056],[3997.31816,1.978],[3996.35379,1.932],[3995.38942,1.746],[3994.42504,1.478]])]
# Repeated DataFrames here for convenience
outxls = os.path.join(os.path.dirname(toplevelname),p+'.xlsx')
workbook = xlsxwriter.Workbook(outxls)
worksheet = workbook.add_worksheet('Sheet1')
worksheet.write(0, 0, 'Bias')
worksheet.write(1, 0, 'Sensitivity')
chart = workbook.add_chart({'type': 'scatter'})
for i, h in enumerate(headers.index):
worksheet.write_number(0, 2*i+2, headers['Bias'][h], )
worksheet.write_number(1, 2*i+2, headers['Sensitivity'][h])
for r, row in dfs[h].iterrows():
for c, col in enumerate(row):
worksheet.write_number(r+2, 2*i+c+1, row[c])
l = len(dfs[h])
chart.add_series({
'name': headers['Bias'][h],
'categories': ['Sheet1', 3, 1, l, 1],
'values': ['Sheet1', 3, 2, l, 2],
})
worksheet.insert_chart('B3', chart)
workbook.close()
Full Error Message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-56283fee8773> in <module>()
52 'name': headers['Bias'][h],
53 'categories': ['Sheet1', 3, 1, l, 1],
---> 54 'values': ['Sheet1', 3, 2, l, 2],
55 })
56 worksheet.insert_chart('B3', chart)
/Users/megablanc/Library/Python/2.7/lib/python/site-packages/xlsxwriter/chart.pyc in add_series(self, options)
134 # Switch name and name_formula parameters if required.
135 name, name_formula = self._process_names(options.get('name'),
--> 136 options.get('name_formula'))
137
138 # Get an id for the data equivalent to the range formula.
/Users/megablanc/Library/Python/2.7/lib/python/site-packages/xlsxwriter/chart.pyc in _process_names(self, name, name_formula)
788 name_formula = quote_sheetname(name[0]) + '!' + cell
789 name = ''
--> 790 elif re.match(r'^=?[^!]+!\$?[A-Z]+\$?[0-9]+', name):
791 # Name looks like a formula, use it to set name_formula.
792 name_formula = name
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.pyc in match(pattern, string, flags)
135 """Try to apply the pattern at the start of the string, returning
136 a match object, or None if no match was found."""
--> 137 return _compile(pattern, flags).match(string)
138
139 def search(pattern, string, flags=0):
TypeError: buffer size mismatch
回答1:
The problem is that the name
value headers['Bias'][h]
isn't a string and raises a TypeError when passed to the function.
The error is a little more confusing than it should be due to the fact that the data type is a numpy.float64
which raises an unusual buffer size mismatch
error.
The fact that the function parameters are spread over several lines also makes it look like the issue is with the values
parameter rather than the name
parameter.
来源:https://stackoverflow.com/questions/30386478/typeerror-buffer-size-mismatch-error-when-defining-a-series-in-xlsxwriter