[
https://issues.apache.org/jira/browse/BEAM-12169?focusedWorklogId=726295&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-726295
]
ASF GitHub Bot logged work on BEAM-12169:
-----------------------------------------
Author: ASF GitHub Bot
Created on: 14/Feb/22 13:24
Start Date: 14/Feb/22 13:24
Worklog Time Spent: 10m
Work Description: yeandy commented on a change in pull request #16677:
URL: https://github.com/apache/beam/pull/16677#discussion_r805836633
##########
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:
If an entry in a series is `np.nan`, and then is converted to dtype
CategoricalDtype, then pandas
[behavior](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html)
is to propogate the NaN. Example:
```
>>>s = pd.Series(
[
"this is a regular sentence",
"https://docs.python.org/3/tutorial/index.html",
np.nan
]
)
>>>s.str.split(expand=True)
0 1 2 3
4
0 this is a regular
sentence
1 https://docs.python.org/3/tutorial/index.html None None None
None
2 NaN NaN NaN NaN
NaN
```
In row 1, where which the string does not get split, in order to propagate
`None` into other columns, I do `.replace(np.nan, value=None)`. However this
makes row 2 be all `None` instead of `NaN`.
Is there a way to only choose specific rows to be `NaN` and not `None`?
i.e. use `s.isna()` to find those indices?
--
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: 726295)
Time Spent: 4h (was: 3h 50m)
> 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: 4h
> 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)