yeandy commented on a change in pull request #17043:
URL: https://github.com/apache/beam/pull/17043#discussion_r837878672
##########
File path: sdks/python/apache_beam/dataframe/frames.py
##########
@@ -3655,6 +3656,121 @@ def shift(self, axis, freq, **kwargs):
describe = _agg_method(pd.DataFrame, 'describe')
max = _agg_method(pd.DataFrame, 'max')
min = _agg_method(pd.DataFrame, 'min')
+
+ @frame_base.with_docs_from(pd.DataFrame)
+ @frame_base.args_to_kwargs(pd.DataFrame)
+ @frame_base.populate_defaults(pd.DataFrame)
+ def pivot(self, index=None, columns=None, values=None, **kwargs):
+ def verify_all_categorical(all_cols_are_categorical):
+ if not all_cols_are_categorical:
+ raise frame_base.WontImplementError(
+ "pivot() 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")
+
+ # Construct column index
+ if is_list_like(columns) and len(columns) <= 1:
+ columns = columns[0]
+ selected_cols = self._expr.proxy()[columns]
+ if isinstance(selected_cols, pd.Series):
+ all_cols_are_categorical = isinstance(
+ selected_cols.dtype, pd.CategoricalDtype
+ )
+ verify_all_categorical(all_cols_are_categorical)
+
+ # If values not provided, take all remaining columns of dataframe
+ if not values:
+ values = self._expr.proxy() \
+ .drop(index, axis=1).drop(columns, axis=1).columns.values
+
+ # Take the provided values
+ if is_list_like(values) and len(values) > 1:
+ values_in_col_index = values
+ names = [None, columns]
+ col_index = pd.MultiIndex.from_product(
+ [values_in_col_index,
+ selected_cols.dtypes.categories.astype('category')],
+ names=names
+ )
+ else:
+ col_index = pd.CategoricalIndex(
+ selected_cols.dtype.categories,
+ name=columns
+ )
+ else:
+ all_cols_are_categorical = all(
+ isinstance(c, pd.CategoricalDtype) for c in selected_cols.dtypes
+ )
+ verify_all_categorical(all_cols_are_categorical)
+
+ categories = [
+ c.categories.astype('category') for c in selected_cols.dtypes
+ ]
+ if is_list_like(columns) and len(columns) > 1:
+ col_index = pd.MultiIndex.from_product(categories, names=columns)
+ else:
+ col_index = pd.CategoricalIndex(
+ selected_cols.dtype.categories,
+ name=columns
+ )
+
+ # Construct row index
+ if index:
+ per_partition = expressions.ComputedExpression(
+ 'pivot-per-partition',
+ lambda df: df.set_index(keys=index), [self._expr],
+ preserves_partition_by=partitionings.Singleton(),
+ requires_partition_by=partitionings.Arbitrary()
+ )
+ if is_list_like(index):
Review comment:
I've updated the logic to extract the index from a temporary df created
from calling `pivot` from the current proxy. Thus we don't have to do this
check if `index` is a list or not.
--
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]