Skip to content

Commit c99f867

Browse files
committed
Improve pandas dataframe inspection
1 parent 53eebda commit c99f867

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/inspectorscripts.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def _jupyterlab_variableinspector_getsizeof(x):
7777
elif __torch and isinstance(x, __torch.Tensor):
7878
return x.element_size() * x.nelement()
7979
elif __pd and type(x).__name__ == 'DataFrame':
80-
return x.memory_usage().sum()
80+
# DO NOT CALL df.memory_usage() as this can be very costly
81+
# to the point of crashing the kernel
82+
return "?"
8183
else:
8284
return sys.getsizeof(x)
8385
@@ -136,8 +138,17 @@ def _jupyterlab_variableinspector_getcontentof(x):
136138
content += f'"{key}": {x[key]}'
137139
content += ", ...}"
138140
elif __pd and isinstance(x, __pd.DataFrame):
139-
colnames = ', '.join(x.columns.map(str))
140-
content = "Columns: %s" % colnames
141+
if len(x.columns) <= _jupyterlab_variableinspector_maxitems:
142+
colnames = ', '.join(x.columns.map(str))
143+
content = "Columns: %s" % colnames
144+
else:
145+
content = "Columns: "
146+
for idx in range(_jupyterlab_variableinspector_maxitems):
147+
if idx > 0:
148+
content += ", "
149+
content += str(x.columns[idx])
150+
content += ", ..."
151+
return content
141152
elif __pd and isinstance(x, __pd.Series):
142153
content = str(x.values).replace(" ", ", ")[1:-1]
143154
content = content.replace("\\n", "")

0 commit comments

Comments
 (0)