yeandy commented on a change in pull request #16677:
URL: https://github.com/apache/beam/pull/16677#discussion_r819036224



##########
File path: sdks/python/apache_beam/dataframe/frames.py
##########
@@ -4721,13 +4721,82 @@ def repeat(self, repeats):
       pd.core.strings.StringMethods, 'get_dummies',
       reason='non-deferred-columns')
 
-  split = frame_base.wont_implement_method(
-      pd.core.strings.StringMethods, 'split',
-      reason='non-deferred-columns')
+  def _split_helper(
+    self, rsplit=False, pat=None, expand=False, regex=None, **kwargs):
+    if not expand:
+      # Not creating separate columns
+      proxy = self._expr.proxy()
+      func = lambda s: pd.concat([proxy,
+        (s.str.split(pat=pat, expand=expand, regex=regex, **kwargs)
+        if not rsplit else s.str.rsplit(pat=pat, expand=expand, **kwargs))]
+      )
+    else:
+      # Creating separate columns, so must be more strict on dtype
+      dtype = self._expr.proxy().dtype
+      if not isinstance(dtype, pd.CategoricalDtype):
+        method_name = 'rsplit' if rsplit else 'split'
+        raise frame_base.WontImplementError(
+            method_name + "() of non-categorical type is not supported because 
"
+            "the type of the output column depends on the data. Please use "
+            "pd.CategoricalDtype with explicit categories.",
+            reason="non-deferred-columns")
 
-  rsplit = frame_base.wont_implement_method(
-      pd.core.strings.StringMethods, 'rsplit',
-      reason='non-deferred-columns')
+      if regex is False or (
+        regex is None and isinstance(pat, str) and len(pat) == 1):
+        # Treat pat as literal string
+        split_cats = [
+          cat.split(
+            sep=kwargs.get('pat'),
+            maxsplit=kwargs.get('n', -1)
+          ) for cat in dtype.categories
+        ]
+      else:
+        # Treat pat as regex
+        split_cats = [
+          re.split(
+            pattern=pat,
+            string=cat,
+            maxsplit=kwargs.get('n', 0)
+          ) for cat in dtype.categories
+        ]
+
+      max_splits = len(max(split_cats, key=len))
+      proxy = pd.DataFrame(columns=range(max_splits))
+
+      func = lambda s: pd.concat([proxy,
+        (s.str.split(pat=pat, expand=expand, regex=regex, **kwargs)
+        if not rsplit else s.str.rsplit(pat=pat, expand=expand, **kwargs))]
+      ).replace(np.nan, value=None)

Review comment:
       Sounds good! I was playing around with very similar logic to this, and 
was running into issues passing indices `~s.isna()` into the original series 
and doing subsequent updates to the result. So thought that it wasn't possible. 
I guess it there was some sort of oversight of mine.




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