TheNeuralBit commented on a change in pull request #15827:
URL: https://github.com/apache/beam/pull/15827#discussion_r765960282



##########
File path: sdks/python/apache_beam/dataframe/frames.py
##########
@@ -3547,6 +3616,110 @@ def value_counts(self, subset=None, sort=False, 
normalize=False,
         return result
 
 
+  @frame_base.with_docs_from(pd.DataFrame)
+  @frame_base.args_to_kwargs(pd.DataFrame)
+  @frame_base.populate_defaults(pd.DataFrame)
+  def idxmin(self, **kwargs):
+    axis = kwargs.get('axis', 0)
+
+    index_dtype = self._expr.proxy().index.dtype
+    columns_dtype = self._expr.proxy().columns.dtype
+
+    def compute_idxmin(df):
+      min_indexes = df.idxmin(**kwargs).unique()
+      if pd.isna(min_indexes).any():
+        return df
+      else:
+        return df.loc[min_indexes]
+
+    if axis in ('index', 0):
+      requires_partition = partitionings.Singleton()
+
+      proxy_index = pd.Index([], dtype=columns_dtype)
+      proxy = pd.Series([], index=proxy_index, dtype=index_dtype)
+      partition_proxy = self._expr.proxy().copy()
+
+      idxmin_per_partition = expressions.ComputedExpression(
+        'idxmin-per-partition',
+        compute_idxmin, [self._expr],
+        proxy=partition_proxy,
+        requires_partition_by=partitionings.Arbitrary(),
+        preserves_partition_by=partitionings.Arbitrary()
+      )
+
+    elif axis in ('columns', 1):
+      requires_partition=partitionings.Index()
+
+      proxy_index = pd.Index([], dtype=index_dtype)
+      proxy = pd.Series([], index=proxy_index, dtype=columns_dtype)
+
+      idxmin_per_partition = self._expr
+
+
+    with expressions.allow_non_parallel_operations(True):
+      return frame_base.DeferredFrame.wrap(
+        expressions.ComputedExpression(
+          'idxmin',
+          lambda df: df.idxmin(**kwargs), [idxmin_per_partition],
+          proxy=proxy,
+          requires_partition_by=requires_partition,
+          preserves_partition_by=partitionings.Singleton()
+        )
+      )
+
+
+  @frame_base.with_docs_from(pd.DataFrame)
+  @frame_base.args_to_kwargs(pd.DataFrame)
+  @frame_base.populate_defaults(pd.DataFrame)
+  def idxmax(self, **kwargs):

Review comment:
       You might consider adding a helper (here and in Series) like `def 
_idxmaxmin_helper(self, op, kwargs):` (where op is 'idxmin' or 'idxmax'). That 
could let you implement the common logic in one place. I'll leave that up to 
you though.

##########
File path: sdks/python/apache_beam/dataframe/frames.py
##########
@@ -3547,6 +3616,110 @@ def value_counts(self, subset=None, sort=False, 
normalize=False,
         return result
 
 
+  @frame_base.with_docs_from(pd.DataFrame)
+  @frame_base.args_to_kwargs(pd.DataFrame)
+  @frame_base.populate_defaults(pd.DataFrame)
+  def idxmin(self, **kwargs):
+    axis = kwargs.get('axis', 0)
+
+    index_dtype = self._expr.proxy().index.dtype
+    columns_dtype = self._expr.proxy().columns.dtype
+
+    def compute_idxmin(df):
+      min_indexes = df.idxmin(**kwargs).unique()
+      if pd.isna(min_indexes).any():
+        return df
+      else:
+        return df.loc[min_indexes]
+
+    if axis in ('index', 0):
+      requires_partition = partitionings.Singleton()
+
+      proxy_index = pd.Index([], dtype=columns_dtype)
+      proxy = pd.Series([], index=proxy_index, dtype=index_dtype)
+      partition_proxy = self._expr.proxy().copy()
+
+      idxmin_per_partition = expressions.ComputedExpression(
+        'idxmin-per-partition',
+        compute_idxmin, [self._expr],
+        proxy=partition_proxy,
+        requires_partition_by=partitionings.Arbitrary(),
+        preserves_partition_by=partitionings.Arbitrary()
+      )
+
+    elif axis in ('columns', 1):

Review comment:
       nit: please add an else to raise a ValueError for an invalid axis

##########
File path: sdks/python/apache_beam/dataframe/frames.py
##########
@@ -1277,6 +1277,74 @@ def align(self, other, join, axis, level, method, 
**kwargs):
       requires_partition_by=partitionings.Arbitrary(),
       preserves_partition_by=partitionings.Singleton())
 
+  @frame_base.with_docs_from(pd.Series)
+  @frame_base.args_to_kwargs(pd.Series)
+  @frame_base.populate_defaults(pd.Series)
+  def idxmin(self, **kwargs):
+    def compute_idxmin(s):
+      min_index = s.idxmin(**kwargs)
+      if pd.isna(min_index):
+        return s
+      else:
+        return s.loc[[min_index]]
+
+    # Avoids empty Series error when evaluating proxy
+    index_dtype = self._expr.proxy().index.dtype
+    index = pd.Index([], dtype=index_dtype)
+    proxy = self._expr.proxy().copy()
+    proxy.index = index
+    proxy = proxy.append(
+        pd.Series([np.inf], index=np.asarray(['0']).astype(proxy.index.dtype)))
+
+    idx_min = expressions.ComputedExpression(
+        'idx_min',
+        compute_idxmin, [self._expr],
+        proxy=proxy,
+        requires_partition_by=partitionings.Index(),
+        preserves_partition_by=partitionings.Arbitrary())
+
+    with expressions.allow_non_parallel_operations(True):
+      return frame_base.DeferredFrame.wrap(
+          expressions.ComputedExpression(
+              'idxmin_combine',
+              lambda s: s.idxmin(**kwargs), [idx_min],
+              requires_partition_by=partitionings.Singleton(),
+              preserves_partition_by=partitionings.Singleton()))
+
+  @frame_base.with_docs_from(pd.Series)
+  @frame_base.args_to_kwargs(pd.Series)
+  @frame_base.populate_defaults(pd.Series)
+  def idxmax(self, **kwargs):
+    def compute_idxmax(s):
+      max_index = s.idxmax(**kwargs)
+      if pd.isna(max_index):
+        return s
+      else:
+        return s.loc[[max_index]]
+
+    # Avoids empty Series error when evaluating proxy
+    index_dtype = self._expr.proxy().index.dtype
+    index = pd.Index([], dtype=index_dtype)
+    proxy = self._expr.proxy().copy()
+    proxy.index = index
+    proxy = proxy.append(
+        pd.Series([-np.inf], 
index=np.asarray(['0']).astype(proxy.index.dtype)))
+
+    idx_max = expressions.ComputedExpression(
+        'idx_max',
+        compute_idxmax, [self._expr],
+        proxy=proxy,
+        requires_partition_by=partitionings.Index(),

Review comment:
       Hm we should dig into why you're getting an error without this. I'll 
take a look




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to