-
-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Notifying a trait with a DataFrame instance throws Value Error #756
Comments
I've run into the same issue when trying to observe a I am not sure this is really a traitlets problem... but more the "bad behaviour" of the DataFrame I've worked around it for my case by overriding the
(although this is causing other headaches... ) |
I'd recommend overriding this value-based comparison behavior of If you'd like to make a contribution, it would be better if |
EDIT (WARNING): It does not work when multiple observers on the trait, since the raised error will still interrupt the loop over callbacks ... I found a workaround by creating a dataframe trait type that ignores that particular error: class TraitletsPandasDataFrame(traitlets.Instance):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, klass=pd.DataFrame, **kwargs)
def __set__(self, obj: traitlets.HasTraits, value) -> None:
# Ignore error raised by old and new dataframes comparison
# see https://github.com/ipython/traitlets/issues/756
try:
super().__set__(obj, value)
except ValueError as e:
if not (
len(e.args) > 0
and isinstance(e.args[0], str)
and e.args[0].startswith("The truth value of a DataFrame is ambiguous.")
):
raise e
class SomeClass(traitlets.HasTraits):
df = TraitletsPandasDataFrame() |
Hey there,
At first, thank you for this amazing library!
I noticed that there are problems when linking multiple objects using the link function, if the value of a trait is a dataframe.
To me it looks like the compare logic overridden by pandas is causing the problem.
Here is an example:
Stacktrace:
As you can see, hhe problem is in
traitlets.py: 366
, becausegetattr(self.source[0], self.source[1]) != change.new
does not return a bool value in the case of a DataFrame.Would it be possible to make this function compatible with pandas, or possibly define a custom function for comparison?
Thank you in advance!
The text was updated successfully, but these errors were encountered: