You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/06/07 16:53:05 UTC

[GitHub] [beam] rohdesamuel commented on a change in pull request #14922: [BEAM-9547] Add support for `DataFrame.value_counts()`

rohdesamuel commented on a change in pull request #14922:
URL: https://github.com/apache/beam/pull/14922#discussion_r646776193



##########
File path: sdks/python/apache_beam/dataframe/frames.py
##########
@@ -458,6 +458,32 @@ def size(self):
               requires_partition_by=partitionings.Singleton(),
               preserves_partition_by=partitionings.Singleton()))
 
+  def length(self):
+    """Alternative to ``len(df)`` which returns a deferred result that can be
+    used in arithmetic with :class:`DeferredSeries` or
+    :class:`DeferredDataFrame` instances."""
+    lengths = expressions.ComputedExpression(
+        'get_lengths',
+        # Wrap scalar results in a Series for easier concatenation later
+        lambda df: pd.Series(len(df)),
+        [self._expr],
+        requires_partition_by=partitionings.Arbitrary(),
+        preserves_partition_by=partitionings.Singleton())
+
+    with expressions.allow_non_parallel_operations(True):
+      return frame_base.DeferredFrame.wrap(
+          expressions.ComputedExpression(
+              'sum_lengths',
+              lambda lengths: lengths.sum(), [lengths],
+              requires_partition_by=partitionings.Singleton(),
+              preserves_partition_by=partitionings.Singleton()))
+
+  def __len__(self):
+    raise frame_base.WontImplementError(
+        "len(df) is not currently supported because it produces a non-deferred "
+        "result. Consider using df.length() instead.",
+        reason="non-deferred-result")

Review comment:
       ohh that's really weird! Good to know though




-- 
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.

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