问题描述
我正在尝试使用xlsxwriter和pandas数据以编程方式在xlsx文件中创建图表.编写单元格可以成功,但是,当我尝试为散点图定义数据系列时,出现TypeError缓冲区大小不匹配",并且可以确定我定义的范围是可以接受的.
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.
示例代码:
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()
完整错误消息:
---------------------------------------------------------------------------
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
推荐答案
问题是name
值headers['Bias'][h]
不是字符串,并且在传递给函数时会引发TypeError.
The problem is that the name
value headers['Bias'][h]
isn't a string and raises a TypeError when passed to the function.
由于数据类型为numpy.float64
会引发异常的buffer size mismatch
错误,因此该错误比应该引起的混乱还要多.
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.
函数参数分布在多行上的事实也使问题似乎出在values
参数而不是name
参数上.
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.
这篇关于在xlsxwriter中定义序列时发生TypeError'缓冲区大小不匹配'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!