You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/12/17 06:03:52 UTC

[GitHub] [spark] HyukjinKwon commented on a change in pull request #34931: [SPARK-37657][PYTHON] Support str and timestamp for (Series|DataFrame).describe()

HyukjinKwon commented on a change in pull request #34931:
URL: https://github.com/apache/spark/pull/34931#discussion_r771124419



##########
File path: python/pyspark/pandas/frame.py
##########
@@ -8828,22 +8842,117 @@ def describe(self, percentiles: Optional[List[float]] = None) -> "DataFrame":
         else:
             percentiles = [0.25, 0.5, 0.75]
 
-        formatted_perc = ["{:.0%}".format(p) for p in sorted(percentiles)]
-        stats = ["count", "mean", "stddev", "min", *formatted_perc, "max"]
+        if len(exprs_numeric) == 0:
+            if len(exprs_non_numeric) == 0:
+                raise ValueError("Cannot describe a DataFrame without columns")
 
-        sdf = self._internal.spark_frame.select(*exprs).summary(*stats)
-        sdf = sdf.replace("stddev", "std", subset=["summary"])
+            # Handling non-numeric type columns
+            # We will retrive the `count`, `unique`, `top` and `freq`.
+            sdf = self._internal.spark_frame.select(*exprs_non_numeric)
 
-        internal = InternalFrame(
-            spark_frame=sdf,
-            index_spark_columns=[scol_for(sdf, "summary")],
-            column_labels=column_labels,
-            data_spark_columns=[
-                scol_for(sdf, self._internal.spark_column_name_for(label))
-                for label in column_labels
-            ],
-        )
-        return DataFrame(internal).astype("float64")
+            # Get `count` & `unique` for each columns
+            has_timestamp_type = True in is_timestamp_types
+            if not has_timestamp_type:
+                counts, uniques = map(
+                    lambda x: x[1:], sdf.summary("count", "count_distinct").take(2)
+                )
+            else:
+                # `summary` doesn't support for timestamp column, so we should manually compute it
+                # if timestamp type column exists.
+                counts = []
+                uniques = []
+                exprs = []
+                for column in exprs_non_numeric:
+                    exprs.append(F.count(column))
+                    exprs.append(F.count_distinct(column))
+
+                count_unique_values = list(sdf.select(*exprs).first().asDict().values())
+                for i in range(0, len(count_unique_values) - 1, 2):
+                    counts.append(str(count_unique_values[i]))
+                    uniques.append(str(count_unique_values[i + 1]))
+
+            # Get `top` & `freq` for each columns
+            tops = []
+            freqs = []
+            for column in exprs_non_numeric:
+                top, freq = (
+                    sdf.groupby(column)
+                    .count()
+                    .sort("count", ascending=False)
+                    .first()
+                    .asDict()
+                    .values()
+                )
+                tops.append(str(top))
+                freqs.append(str(freq))
+
+            stats = [counts, uniques, tops, freqs]
+            stats_names = ["count", "unique", "top", "freq"]
+
+            # Get `first` & `last` for each columns if timestamp type column exists.
+            if has_timestamp_type:
+                exprs = []
+                for is_timestamp_type, column, column_name in zip(
+                    is_timestamp_types, exprs_non_numeric, column_names
+                ):
+                    if is_timestamp_type:
+                        # `first` & `last` are min & max respectively for timestamp type.
+                        exprs.append(F.min(column))
+                        exprs.append(F.max(column))
+                    else:
+                        # `first` & `last` are always NaN for string type columns.
+                        exprs.append(F.lit(np.NaN).alias(column_name + "_first"))
+                        exprs.append(F.lit(np.NaN).alias(column_name + "_last"))
+
+                firsts = []
+                lasts = []
+                first_last_values = list(sdf.select(*exprs).first().asDict().values())
+                for i in range(0, len(first_last_values) - 1, 2):
+                    first_value = first_last_values[i]
+                    last_value = first_last_values[i + 1]
+                    print(first_value)

Review comment:
       can we remove this print




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org