Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/pyspark/sql/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ def isLocal(self):
"""
return self._jdf.isLocal()

def show(self):
def show(self, n=20):
"""
Print the first 20 rows.
Print the first n rows.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there isn't n here. maybe rename numRows -> n


>>> df
DataFrame[age: int, name: string]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more thing. can you add one more docstring test to test n = 1?

Expand All @@ -283,7 +283,7 @@ def show(self):
2 Alice
5 Bob
"""
print self._jdf.showString().encode('utf8', 'ignore')
print self._jdf.showString(n).encode('utf8', 'ignore')

def __repr__(self):
return "DataFrame[%s]" % (", ".join("%s: %s" % c for c in self.dtypes))
Expand Down
13 changes: 10 additions & 3 deletions sql/core/src/main/scala/org/apache/spark/sql/DataFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ class DataFrame protected[sql](

/**
* Internal API for Python
* @param numRows Number of rows to show
*/
private[sql] def showString(): String = {
val data = take(20)
private[sql] def showString(numRows: Int): String = {
val data = take(numRows)
val numCols = schema.fieldNames.length

// For cells that are beyond 20 characters, replace it with the first 17 and "..."
Expand Down Expand Up @@ -293,9 +294,15 @@ class DataFrame protected[sql](
* 1983 03 0.410516 0.442194
* 1984 04 0.450090 0.483521
* }}}
* @param numRows Number of rows to show
* @group basic
*/
def show(): Unit = println(showString())
def show(numRows: Int): Unit = println(showString(numRows))

/**
* Displays the top 20 rows of [[DataFrame]] in a tabular form.
*/
def show(): Unit = show(20)

/**
* Cartesian join with another [[DataFrame]].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import org.apache.spark.sql.*;
Expand Down Expand Up @@ -81,4 +82,12 @@ public void testVarargMethods() {
df.groupBy().agg(countDistinct(col("key"), col("value")));
df.select(coalesce(col("key")));
}

@Ignore
public void testShow() {
// This test case is intended ignored, but to make sure it compiles correctly
DataFrame df = context.table("testData");
df.show();
df.show(1000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,9 @@ class DataFrameSuite extends QueryTest {
checkAnswer(df.select(df("key")), testData.select('key).collect().toSeq)
}

ignore("show") {
// This test case is intended ignored, but to make sure it compiles correctly
testData.select($"*").show()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this is a bad idea. it prints a lot of garbage to console. maybe we can ignore the test and add a comment saying this is ignored but we want to make sure it compiles correctly. do the same for java

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I realized it is not good, it's not checking any answer. I will modify it later

testData.select($"*").show(1000)
}
}