@@ -77,7 +77,12 @@ def _jupyterlab_variableinspector_getsizeof(x):
77
77
elif __torch and isinstance(x, __torch.Tensor):
78
78
return x.element_size() * x.nelement()
79
79
elif __pd and type(x).__name__ == 'DataFrame':
80
- return x.memory_usage().sum()
80
+ # DO NOT CALL df.memory_usage() for big dataframes as this can be very costly
81
+ # to the point of making the kernel unresponsive or crashing it
82
+ if len(x.columns) < 10_000:
83
+ return x.memory_usage().sum()
84
+ else:
85
+ return "?"
81
86
else:
82
87
return sys.getsizeof(x)
83
88
@@ -136,8 +141,17 @@ def _jupyterlab_variableinspector_getcontentof(x):
136
141
content += f'"{key}": {x[key]}'
137
142
content += ", ...}"
138
143
elif __pd and isinstance(x, __pd.DataFrame):
139
- colnames = ', '.join(x.columns.map(str))
140
- content = "Columns: %s" % colnames
144
+ if len(x.columns) <= _jupyterlab_variableinspector_maxitems:
145
+ colnames = ', '.join(x.columns.map(str))
146
+ content = "Columns: %s" % colnames
147
+ else:
148
+ content = "Columns: "
149
+ for idx in range(_jupyterlab_variableinspector_maxitems):
150
+ if idx > 0:
151
+ content += ", "
152
+ content += str(x.columns[idx])
153
+ content += ", ..."
154
+ return content
141
155
elif __pd and isinstance(x, __pd.Series):
142
156
content = str(x.values).replace(" ", ", ")[1:-1]
143
157
content = content.replace("\\n", "")
0 commit comments