educative.io

Error in test challenge

The solution and the test-array contains an error. It misses leap years. It is also easy to see that the last element in the test array (i.e., “Hale Cole,“Atlanta, GA”,2000-10-24,2021-10-20,”$54,500",engineering"), which according to the test array is supposed to be equal to 21. It should be 20

It seems that this problem is relatively hard to solve correctly. But the following should do the job but is unfortunately bulky.

staff["age"] = staff["start_date"].dt.year - staff["date_of_birth"].dt.year - ((staff["start_date"].dt.month - staff["date_of_birth"].dt.month < 0) | ((staff["start_date"].dt.month - staff["date_of_birth"].dt.month == 0) & (staff["start_date"].dt.day - staff["date_of_birth"].dt.day < 0))) * 1

Course: Mastering Data Analysis with Python Pandas - Learn Interactively
Lesson: Challenge: The Age of Employees - Mastering Data Analysis with Python Pandas


Course: Mastering Data Analysis with Python Pandas - Learn Interactively
Lesson: Challenge: The Age of Employees - Mastering Data Analysis with Python Pandas

Thank you for providing your feedback and highlighting the concerns regarding the age calculation solution for the staff dataset.

I appreciate your attention to detail. Upon further review, I understand that the initial solution overlooks leap years, which could affect the accuracy of age calculations. While the problem statement specifies calculating age in whole years, it’s important to ensure the solution handles leap years correctly for comprehensive accuracy.

In response to your feedback, I have revised the solution to address both concerns:

import pandas as pd

staff = pd.read_csv("staff.csv")

staff = staff.astype({
    "date_of_birth": "datetime64[ns]",
    "start_date": "datetime64[ns]"
})

def find_age():
    try:
        # Calculate age by taking the difference in years between start_date and date_of_birth
        staff["age"] = staff["start_date"].dt.year - staff["date_of_birth"].dt.year
        # Adjust age for cases where start_date falls before the birthday in the same year
        staff.loc[(staff["start_date"].dt.month < staff["date_of_birth"].dt.month) | 
                  ((staff["start_date"].dt.month == staff["date_of_birth"].dt.month) & 
                  (staff["start_date"].dt.day < staff["date_of_birth"].dt.day)), "age"] -= 1
        return list(staff["age"])
        
    except Exception as e:
        print("An error occurred:", e)

This revised solution should accurately calculate the age of employees at the time they were hired, considering both leap years and cases where the start date falls before the birthday in the same year.

Thank you once again for bringing these concerns to our attention. Your feedback is invaluable in improving our solutions. We’ll fix the test cases soon. If you have any further questions or suggestions, please feel free to share them.

1 Like

Hi Komal, Thank you for the fixing the solution. I like your split calculation with the separate part for adjustment better than what I proposed. Looks very nice!

Best regards
Pierre

1 Like