Skip to content

Commit cf8b0dc

Browse files
authored
Merge pull request #319 from martinRenou/improve_df_inspect
Improve pandas dataframe inspection
2 parents 53eebda + 722d65d commit cf8b0dc

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/inspectorscripts.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ 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() 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 "?"
8186
else:
8287
return sys.getsizeof(x)
8388
@@ -136,8 +141,17 @@ def _jupyterlab_variableinspector_getcontentof(x):
136141
content += f'"{key}": {x[key]}'
137142
content += ", ...}"
138143
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
141155
elif __pd and isinstance(x, __pd.Series):
142156
content = str(x.values).replace(" ", ", ")[1:-1]
143157
content = content.replace("\\n", "")

0 commit comments

Comments
 (0)