Skip to content

Commit

Permalink
[ZEPPELIN-5875] Add: z.show works with subtypes of DataFrame (#4683)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-koch authored Nov 8, 2023
1 parent 579074e commit 4352a10
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/src/main/resources/python/zeppelin_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def getDefaultChecked(self, defaultChecked):
def show(self, p, **kwargs):
if hasattr(p, '__name__') and p.__name__ == "matplotlib.pyplot":
self.show_matplotlib(p, **kwargs)
elif type(p).__name__ == "DataFrame": # does not play well with sub-classes
elif any(t.__name__ == 'DataFrame' for t in type(p).mro()):
# `isinstance(p, DataFrame)` would req `import pandas.core.frame.DataFrame`
# and so a dependency on pandas
self.show_dataframe(p, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,22 @@ public void testZeppelinContext() throws InterpreterException, InterruptedExcept
assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
assertEquals("id\tname\n1\ta a\n2\tb b\n3\tc c\n", interpreterResultMessages.get(0).getData());

// Pandas DataFrame with sub type
context = getInterpreterContext();
result = interpreter.interpret("import pandas as pd\n" +
"class ExtendedDataFrame(pd.DataFrame):\n" +
" pass\n" +
"df = ExtendedDataFrame({'id':[1,2,3], 'name':['a\ta','b\\nb','c\\r\\nc']})\n" +
"z.show(df)",
context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code(),
context.out.toInterpreterResultMessage().toString());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
assertEquals("id\tname\n1\ta a\n2\tb b\n3\tc c\n", interpreterResultMessages.get(0).getData());

// Pandas DataFrame limited to three results
context = getInterpreterContext();
result = interpreter.interpret("import pandas as pd\n" +
"df = pd.DataFrame({'id':[1,2,3,4], 'name':['a','b','c', 'd']})\nz.show(df)", context);
Expand Down

0 comments on commit 4352a10

Please sign in to comment.