[ 
https://issues.apache.org/jira/browse/BEAM-12169?focusedWorklogId=727460&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-727460
 ]

ASF GitHub Bot logged work on BEAM-12169:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 15/Feb/22 18:52
            Start Date: 15/Feb/22 18:52
    Worklog Time Spent: 10m 
      Work Description: yeandy commented on a change in pull request #16615:
URL: https://github.com/apache/beam/pull/16615#discussion_r806222736



##########
File path: sdks/python/apache_beam/dataframe/transforms_test.py
##########
@@ -73,29 +74,52 @@ def df_equal_to(expected):
 
 
 class TransformTest(unittest.TestCase):
-  def run_scenario(self, input, func):
+  def run_scenario(self, input, func, check_subset=False):
+    """
+    In order for us to perform non-deferred operations, we have
+    to enumerate all possible categories, even if they are
+    unobserved. The default Pandas implementation on the other hand
+    does not produce unobserved columns. This means when conducting
+    tests, we need to account for the fact that the Beam result may
+    be a superset of that of the Pandas result.
+
+    When ``check_subset`` is set to ``True``, we will only
+    check that all of the columns in the Pandas implementation is
+    contained in that of the Beam implementation.

Review comment:
       No worries! The logic I added can support verifying if they are all 0s 
or NaNs (or other values), depending on the expected operation behavior. 

##########
File path: sdks/python/apache_beam/dataframe/frames_test.py
##########
@@ -1950,9 +1950,55 @@ class BeamSpecificTest(unittest.TestCase):
   """Tests for functionality that's specific to the Beam DataFrame API.
 
   These features don't exist in pandas so we must verify them independently."""
-  def assert_frame_data_equivalent(self, actual, expected):
+  def assert_frame_data_equivalent(
+      self, actual, expected, check_subset=False, extra_col_value=0):
     """Verify that actual is the same as expected, ignoring the index and order
-    of the data."""
+    of the data.
+
+    Note: In order for us to perform non-deferred operations in Beam, we have

Review comment:
       Fixed

##########
File path: sdks/python/apache_beam/dataframe/frames_test.py
##########
@@ -1950,9 +1950,55 @@ class BeamSpecificTest(unittest.TestCase):
   """Tests for functionality that's specific to the Beam DataFrame API.
 
   These features don't exist in pandas so we must verify them independently."""
-  def assert_frame_data_equivalent(self, actual, expected):
+  def assert_frame_data_equivalent(
+      self, actual, expected, check_subset=False, extra_col_value=0):
     """Verify that actual is the same as expected, ignoring the index and order
-    of the data."""
+    of the data.
+
+    Note: In order for us to perform non-deferred operations in Beam, we have
+    to enumerate all possible categories of data, even if they are ultimately
+    unobserved. The default Pandas implementation on the other hand does not
+    produce unobserved columns. This means when conducting tests, we need to
+    account for the fact that the Beam result may be a superset of that of the
+    Pandas result.
+
+    If ``check_subset`` is `True`, we verify that all of the columns in the

Review comment:
       Fixed

##########
File path: sdks/python/apache_beam/dataframe/frames_test.py
##########
@@ -1950,9 +1950,55 @@ class BeamSpecificTest(unittest.TestCase):
   """Tests for functionality that's specific to the Beam DataFrame API.
 
   These features don't exist in pandas so we must verify them independently."""
-  def assert_frame_data_equivalent(self, actual, expected):
+  def assert_frame_data_equivalent(
+      self, actual, expected, check_subset=False, extra_col_value=0):
     """Verify that actual is the same as expected, ignoring the index and order
-    of the data."""
+    of the data.
+
+    Note: In order for us to perform non-deferred operations in Beam, we have
+    to enumerate all possible categories of data, even if they are ultimately
+    unobserved. The default Pandas implementation on the other hand does not
+    produce unobserved columns. This means when conducting tests, we need to
+    account for the fact that the Beam result may be a superset of that of the
+    Pandas result.
+
+    If ``check_subset`` is `True`, we verify that all of the columns in the
+    Dataframe returned from the Pandas implementation is contained in the
+    Dataframe created from the Beam implementation.
+
+    We also check if all columns that exist in the Beam implementation but
+    not in the Pandas implementation are all equal to the ``extra_col_value``
+    to ensure that they were not erroneously populated.
+    """
+    if check_subset:
+      if isinstance(expected, pd.DataFrame):
+        expected_cols = set(expected.columns)
+        actual_cols = set(actual.columns)
+        # Verifying that expected columns is a subset of the actual columns
+        if not set(expected_cols).issubset(set(actual_cols)):
+          raise AssertionError(
+              f"Expected columns:\n{expected.columns}\n is not a"
+              f"subset of {actual.columns}.")
+
+        # Verifying that columns that don't exist in expected
+        # are all NaN in actual

Review comment:
       No worries; I forgot to change the wording in this comment. The logic 
I've added supports configurable values, with `0` as the default value to which 
the extra columns should be compared.




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


Issue Time Tracking
-------------------

    Worklog Id:     (was: 727460)
    Time Spent: 6h 40m  (was: 6.5h)

> Allow non-deferred column operations on categorical columns
> -----------------------------------------------------------
>
>                 Key: BEAM-12169
>                 URL: https://issues.apache.org/jira/browse/BEAM-12169
>             Project: Beam
>          Issue Type: Improvement
>          Components: dsl-dataframe, sdk-py-core
>            Reporter: Brian Hulette
>            Assignee: Andy Ye
>            Priority: P3
>              Labels: dataframe-api
>          Time Spent: 6h 40m
>  Remaining Estimate: 0h
>
> There are several operations that we currently disallow because they produce 
> a variable set of columns in the output based on the data 
> (non-deferred-columns). However, for some dtypes (categorical, boolean) we 
> can easily enumerate all the possible values that will be seen at execution 
> time, so we can predict the columns that will be seen.
> Note we still can't implement these operations 100% correctly, as pandas will 
> typically only create columns for the values that are _observed_, while we'd 
> have to create a column for every possible value.
> We should allow these operations in these special cases.
> Operations in this category:
>  - DataFrame.unstack (can work if unstacked level is a categorical or boolean 
> column)
>  - Series.str.get_dummies
>  - Series.str.split
>  - Series.str.rsplit
>  - DataFrame.pivot
>  - DataFrame.pivot_table
>  - len(GroupBy) and ngroups
>  ** if groupers are all categorical _and_ observed=False or all boolean
>  ** Note these two may not actually be equivalent in all cases: 
> [https://github.com/pandas-dev/pandas/issues/26326]



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to