mohamedawnallah opened a new pull request, #33672: URL: https://github.com/apache/beam/pull/33672
## Description In this commit, we enable named aggregation to deferred DataFrame groupby. Fixes #27278 ## Current Behavior Before | After :-------------------------:|:-------------------------:  |  ## Verifying Named Aggregation in Beam DataFrame GroupBy ```python import pandas as pd import apache_beam as beam from apache_beam.dataframe import convert employee_data = { 'Department': ['HR', 'HR', 'Engineering', 'Engineering', 'Sales', 'Sales'], 'Salary': [50000, 55000, 70000, 75000, 60000, 65000], 'Performance_Score': [8, 9, 7, 6, 8, 8], 'Years_of_Experience': [2, 5, 8, 10, 3, 4] } employee_df = pd.DataFrame(employee_data) CSV_HEADER = "Department,Mean_Salary,Median_Performance_Score,"+\ "Total_Years_of_Experience" def process_aggregated_data(row): """Convert aggregated data into a format suitable for CSV output.""" department = row.Department salary = row.Mean_Salary performance_score = row.Median_Performance_Score experience = row.Total_Years_of_Experience return f"{department},{salary},{performance_score},{experience}" with beam.Pipeline() as pipeline: # Convert pandas DataFrame to PCollection. employee_pcollection = convert.to_pcollection(employee_df, pipeline=pipeline) employee_beam_df = convert.to_dataframe(employee_pcollection) # Group by Department and aggregate functions: # mean salary, median performance score, and sum of years of experience with beam.dataframe.allow_non_parallel_operations(): employee_aggregated_df = employee_beam_df.groupby('Department').agg( Mean_Salary=('Salary', 'mean'), Median_Performance_Score=('Performance_Score', 'median'), Total_Years_of_Experience=('Years_of_Experience', 'sum') ).reset_index() # Write the CSV, including header and data. csv_pcollection = ( convert.to_pcollection(employee_aggregated_df) | beam.Map(lambda row: process_aggregated_data(row)) | beam.io.WriteToText( "employee_data_grouped_by_department", file_name_suffix=".csv", header=CSV_HEADER ) ) ``` ------------------------ Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily: - [x] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead. - [ ] Update `CHANGES.md` with noteworthy changes. - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf). See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier). To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md) GitHub Actions Tests Status (on master branch) ------------------------------------------------------------------------------------------------ [](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule) [](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule) [](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule) [](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule) See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI or the [workflows README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) to see a list of phrases to trigger workflows. -- 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]
