Ipywidgets interact won't apply datepicker to DataFrame ("TypeError: 'DataFrame' object is not callable")
Ipywidgets interact won't apply datepicker to DataFrame ("TypeError: 'DataFrame' object is not callable")
我正在构建一个 jupyter 笔记本,它使用一些交互式小部件来帮助移动一些数据帧。我的 jupyter 和 python 经验有限,到目前为止,我的大部分笔记本都在重新创建我之前在一系列 Excel 数据透视表中创建的合并和计算。
第一次尝试交互式小部件效果不错。它调用数据框,使用滑动小部件通过列的结果限制返回的行(作业打开了多少天):
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
@interact
def days_open_widget(column='days_open', x=(0,2000,5)):
return df.loc[df[column] > x]
接下来,我希望能够使用两个日期选择器分别探索 DataFrame,以将结果限制在创建作业的日期。它的一部分在工作,比如日期选择器填充了最早和最晚的日期,但它们没有绑定到数据框。这是代码:
interact(df,
start_date=widgets.DatePicker(value=df.assignment_creation_date.min()),
end_date=widgets.DatePicker(value=df.assignment_creation_date.max()))
Jupyter 显示日期选择器后,出现以下错误,然后显示未过滤的数据框。
TypeError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\ipywidgets\widgets\interaction.py in update(self, *args)
254 value = widget.get_interact_value()
255 self.kwargs[widget._kwarg] = value
--> 256 self.result = self.f(**self.kwargs)
257 show_inline_matplotlib_plots()
258 if self.auto_display and self.result is not None:
TypeError: 'DataFrame' object is not callable
我不确定我错过了什么。我不确定为什么在我之前访问 DataFrame 时尝试访问它会给我一个 "object is not callable" 错误,或者为什么 DatePickers 成功读取了 DataFrame 列,但随后断开连接。
我尝试创建的行为将调用整个数据框,但让我过滤从日期选择器中选择的日期之间的结果行。我错过了什么?
编辑:我知道代码很乱,事后看来,我认为我不应该分别导入 interact、interactive、fixed,interact_manual 然后再导入所有 ipywidgets。
编辑 2:我无法使用 AC24 的解决方案,但我认为这更多是因为我无法正确定义过滤数据帧的函数。我最终找到了 QGrid,它有一个日期选择器和其他过滤器。
有关 QGrid 的更多信息位于 https://qgrid.readthedocs.io/en/latest/,但如果有人对如何定义将数据帧作为函数传递的函数有更明确的描述,请随时添加。
请注意 interact
与 interactive
装饰器的行为不同。使用 interact
时,第一个参数应该是 function/callable,当您更改任何小部件时,它会被调用。您将数据框 df
作为第一个参数传入,因此 interact
试图调用 df
作为具有 start_date 和 end_date 关键字的函数。因此出现错误,因为数据帧不可调用。
查看示例文档,您可能想要构建一个简单的函数来过滤数据框,并将该函数名称用作 interact
.
的第一个参数
https://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html#Basic-interact
我正在构建一个 jupyter 笔记本,它使用一些交互式小部件来帮助移动一些数据帧。我的 jupyter 和 python 经验有限,到目前为止,我的大部分笔记本都在重新创建我之前在一系列 Excel 数据透视表中创建的合并和计算。
第一次尝试交互式小部件效果不错。它调用数据框,使用滑动小部件通过列的结果限制返回的行(作业打开了多少天):
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
@interact
def days_open_widget(column='days_open', x=(0,2000,5)):
return df.loc[df[column] > x]
接下来,我希望能够使用两个日期选择器分别探索 DataFrame,以将结果限制在创建作业的日期。它的一部分在工作,比如日期选择器填充了最早和最晚的日期,但它们没有绑定到数据框。这是代码:
interact(df,
start_date=widgets.DatePicker(value=df.assignment_creation_date.min()),
end_date=widgets.DatePicker(value=df.assignment_creation_date.max()))
Jupyter 显示日期选择器后,出现以下错误,然后显示未过滤的数据框。
TypeError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\ipywidgets\widgets\interaction.py in update(self, *args)
254 value = widget.get_interact_value()
255 self.kwargs[widget._kwarg] = value
--> 256 self.result = self.f(**self.kwargs)
257 show_inline_matplotlib_plots()
258 if self.auto_display and self.result is not None:
TypeError: 'DataFrame' object is not callable
我不确定我错过了什么。我不确定为什么在我之前访问 DataFrame 时尝试访问它会给我一个 "object is not callable" 错误,或者为什么 DatePickers 成功读取了 DataFrame 列,但随后断开连接。
我尝试创建的行为将调用整个数据框,但让我过滤从日期选择器中选择的日期之间的结果行。我错过了什么?
编辑:我知道代码很乱,事后看来,我认为我不应该分别导入 interact、interactive、fixed,interact_manual 然后再导入所有 ipywidgets。
编辑 2:我无法使用 AC24 的解决方案,但我认为这更多是因为我无法正确定义过滤数据帧的函数。我最终找到了 QGrid,它有一个日期选择器和其他过滤器。
有关 QGrid 的更多信息位于 https://qgrid.readthedocs.io/en/latest/,但如果有人对如何定义将数据帧作为函数传递的函数有更明确的描述,请随时添加。
请注意 interact
与 interactive
装饰器的行为不同。使用 interact
时,第一个参数应该是 function/callable,当您更改任何小部件时,它会被调用。您将数据框 df
作为第一个参数传入,因此 interact
试图调用 df
作为具有 start_date 和 end_date 关键字的函数。因此出现错误,因为数据帧不可调用。
查看示例文档,您可能想要构建一个简单的函数来过滤数据框,并将该函数名称用作 interact
.
https://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html#Basic-interact