rohdesamuel commented on a change in pull request #14922:
URL: https://github.com/apache/beam/pull/14922#discussion_r643563820
##########
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:
I think it makes more sense for len(df) to return a deferred result
rather than a separate API call. This is more consistent with the rest of the
deferred API.
--
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:
[email protected]