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
   :-------------------------:|:-------------------------:
   
![before](https://github.com/user-attachments/assets/98494752-9b17-4cde-8b43-7e98de7c3280)
 |  
![after](https://github.com/user-attachments/assets/154f3c12-c129-4c51-90f9-58a71f972b03)
   
   ## 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)
   
------------------------------------------------------------------------------------------------
   [![Build python source distribution and 
wheels](https://github.com/apache/beam/actions/workflows/build_wheels.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python 
tests](https://github.com/apache/beam/actions/workflows/python_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java 
tests](https://github.com/apache/beam/actions/workflows/java_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go 
tests](https://github.com/apache/beam/actions/workflows/go_tests.yml/badge.svg?event=schedule&&?branch=master)](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]

Reply via email to