浮点数和 lambda - 出现类型错误,需要一个浮点数
Float and lambda - getting Type Error, a float is required
我正在尝试获取使用 lambda 函数的 Chartit working with my Django project. I found that it doesn't support datetime on the x axies (it returns an error and says "is not JSON serializable".) So to fix this, I tried to work in this hack。 (我也修改了那里指定的 chartit python 代码。)
这一行给出了错误,"TypeError: a float is required."
x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(i).strftime("%H:%M"), False))
这是函数的相关部分:
cht = Chart(
datasource = happydata,
series_options =
[{'options':{
'type': 'line',
'stacking': False},
'terms':{
'day': [
'rating',]
}}],
chart_options =
{'title': {
'text': 'Ratings'},
'xAxis': {
'title': {
'text': 'Time'}}},
x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(i).strftime("%H:%M"), False))
我阅读了有关 lambda 函数的内容,但我不确定为什么会收到此错误。非常感谢任何帮助!谢谢。
追溯,按要求:
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\MYSTUFF\projects\feels2\feelsapp\views.py" in home
82. x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(i).strftime("%H:%M"), False))
File "C:\Python27\lib\site-packages\chartit\charts.py" in __init__
25. self.generate_plot()
File "C:\Python27\lib\site-packages\chartit\charts.py" in generate_plot
200. data = [(x_mapf(x), y) for (x, y) in data]
File "C:\Users\MYSTUFF\projects\feels2\feelsapp\views.py" in <lambda>
82. x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(i).strftime("%H:%M"), False))
Exception Type: TypeError at /
Exception Value: a float is required
views.py
def home(request):
#Create a DataPool with the data we want to retrieve.
feels = Feel.objects.all()
happydata = \
DataPool(
series=
[{'options': {
'source': Feel.objects.all()},
'terms': [
'day',
'rating']}
])
cht = Chart(
datasource = happydata,
series_options =
[{'options':{
'type': 'line',
'stacking': False},
'terms':{
'day': [
'rating',]
}}],
chart_options =
{'title': {
'text': 'Your Ratings'},
'xAxis': {
'title': {
'text': 'Time'}}},
x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(float(i)).strftime("%H:%M"), False))
return render_to_response('happy.html',{'happychart': cht})
@wwii 是正确的,如果它是给你错误的 lambda,你可能传递了一个字符串。任何int或float应该都可以,try:
lambda i: datetime.fromtimestamp(float(i)).strftime("%H:%M")
编辑:看到你对@wwii 的评论,看起来你可能没有传递实数。您应该尝试在代码中的某处添加打印语句,并在创建该图表之前检查 "i" 的值。
我正在尝试获取使用 lambda 函数的 Chartit working with my Django project. I found that it doesn't support datetime on the x axies (it returns an error and says "is not JSON serializable".) So to fix this, I tried to work in this hack。 (我也修改了那里指定的 chartit python 代码。)
这一行给出了错误,"TypeError: a float is required."
x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(i).strftime("%H:%M"), False))
这是函数的相关部分:
cht = Chart(
datasource = happydata,
series_options =
[{'options':{
'type': 'line',
'stacking': False},
'terms':{
'day': [
'rating',]
}}],
chart_options =
{'title': {
'text': 'Ratings'},
'xAxis': {
'title': {
'text': 'Time'}}},
x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(i).strftime("%H:%M"), False))
我阅读了有关 lambda 函数的内容,但我不确定为什么会收到此错误。非常感谢任何帮助!谢谢。
追溯,按要求:
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\MYSTUFF\projects\feels2\feelsapp\views.py" in home
82. x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(i).strftime("%H:%M"), False))
File "C:\Python27\lib\site-packages\chartit\charts.py" in __init__
25. self.generate_plot()
File "C:\Python27\lib\site-packages\chartit\charts.py" in generate_plot
200. data = [(x_mapf(x), y) for (x, y) in data]
File "C:\Users\MYSTUFF\projects\feels2\feelsapp\views.py" in <lambda>
82. x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(i).strftime("%H:%M"), False))
Exception Type: TypeError at /
Exception Value: a float is required
views.py
def home(request):
#Create a DataPool with the data we want to retrieve.
feels = Feel.objects.all()
happydata = \
DataPool(
series=
[{'options': {
'source': Feel.objects.all()},
'terms': [
'day',
'rating']}
])
cht = Chart(
datasource = happydata,
series_options =
[{'options':{
'type': 'line',
'stacking': False},
'terms':{
'day': [
'rating',]
}}],
chart_options =
{'title': {
'text': 'Your Ratings'},
'xAxis': {
'title': {
'text': 'Time'}}},
x_sortf_mapf_mts=(None, lambda i: datetime.fromtimestamp(float(i)).strftime("%H:%M"), False))
return render_to_response('happy.html',{'happychart': cht})
@wwii 是正确的,如果它是给你错误的 lambda,你可能传递了一个字符串。任何int或float应该都可以,try:
lambda i: datetime.fromtimestamp(float(i)).strftime("%H:%M")
编辑:看到你对@wwii 的评论,看起来你可能没有传递实数。您应该尝试在代码中的某处添加打印语句,并在创建该图表之前检查 "i" 的值。