Skip to content

Commit 4f93eba

Browse files
Make watchpoints work with dataframe (#31)
* Make watchpoints work with dataframe
1 parent 8f4cc79 commit 4f93eba

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/watchpoints/watch_element.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
from .watch_print import WatchPrint
77
import copy
88

9+
try:
10+
import pandas as pd
11+
except ImportError: # pragma: no cover
12+
pd = None
13+
914

1015
class WatchElement:
1116
def __init__(self, frame, node, **kwargs):
@@ -94,6 +99,8 @@ def changed(self, frame):
9499
def obj_changed(self, other):
95100
if not isinstance(self.obj, type(other)):
96101
return True
102+
elif pd is not None and isinstance(self.obj, pd.DataFrame):
103+
return not self.obj.equals(other)
97104
elif self.cmp:
98105
return self.cmp(self.obj, other)
99106
elif self.obj.__class__.__module__ == "builtins":
@@ -110,7 +117,9 @@ def obj_changed(self, other):
110117
return not guess
111118

112119
def update(self):
113-
if self.copy:
120+
if pd is not None and isinstance(self.obj, pd.DataFrame):
121+
self.prev_obj = self.obj.copy(True)
122+
elif self.copy:
114123
self.prev_obj = self.copy(self.obj)
115124
elif self.deepcopy:
116125
self.prev_obj = copy.deepcopy(self.obj)

tests/test_pandas.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __comparison_series__(obj1, obj2):
4343

4444
unwatch()
4545

46-
def test_dataframe(self):
46+
def test_dataframe_cmp(self):
4747
def __comparison_dataframe__(obj1, obj2):
4848
return not obj1.equals(obj2)
4949

@@ -66,3 +66,24 @@ def __comparison_dataframe__(obj1, obj2):
6666
self.assertEqual(cb.counter, 1)
6767

6868
unwatch()
69+
70+
def test_dataframe(self):
71+
cb = CB()
72+
73+
df = pd.DataFrame(
74+
data=[[1, 2], [3, 4], [5, 6]], index=list("abc"), columns=list("AB")
75+
)
76+
77+
watch(df, callback=cb)
78+
79+
# Other stuff happens
80+
a = 2
81+
_ = a + 5
82+
83+
self.assertEqual(cb.counter, 0)
84+
85+
df.loc["a", "B"] = 10
86+
87+
self.assertEqual(cb.counter, 1)
88+
89+
unwatch()

tests/test_watch_element.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import unittest
66
import inspect
77
import os
8+
import pandas as pd
89
from watchpoints.watch_element import WatchElement
910
from watchpoints.util import getargnodes
1011

@@ -169,6 +170,15 @@ def __eq__(self, other):
169170
obj_eq.a["a"] = 3
170171
self.assertTrue(wobj_eq.changed(frame)[0])
171172

173+
def test_dataframe(self):
174+
frame = inspect.currentframe()
175+
df = pd.DataFrame([1, 2, 3])
176+
lst = self.helper(df)
177+
wedf = lst[0]
178+
self.assertFalse(wedf.changed(frame)[0])
179+
df.iloc[0] = 0
180+
self.assertTrue(wedf.changed(frame)[0])
181+
172182
def test_global_module(self):
173183
os.environ['a'] = 'test'
174184
self.helper(os.environ['a'])

0 commit comments

Comments
 (0)