diff --git a/src/test/kotlin/g2801_2900/s2884_modify_columns/solution_test.py b/src/test/kotlin/g2801_2900/s2884_modify_columns/solution_test.py new file mode 100644 index 00000000..8269f9e4 --- /dev/null +++ b/src/test/kotlin/g2801_2900/s2884_modify_columns/solution_test.py @@ -0,0 +1,93 @@ +import pandas as pd +import unittest + +# The function to be tested +def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame: + employees['salary'] = employees['salary'] * 2 + return employees + +# Test class +class TestDropMissingData(unittest.TestCase): + + def test_modify_salary_column_basic_case(self): + # Input DataFrame + employees = pd.DataFrame({ + 'name': ['Jack', 'Piper', 'Mia', 'Ulysses'], + 'salary': [19666, 74754, 62509, 54866] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'name': ['Jack', 'Piper', 'Mia', 'Ulysses'], + 'salary': [39332, 149508, 125018, 109732] + }) + + # Call the function and assert equality + result = modifySalaryColumn(employees) + pd.testing.assert_frame_equal(result, expected_output) + + def test_modify_salary_column_empty_dataframe(self): + # Input: Empty DataFrame + employees = pd.DataFrame(columns=['name', 'salary']) + + # Expected output: Empty DataFrame + expected_output = pd.DataFrame(columns=['name', 'salary']) + + # Call the function and assert equality + result = modifySalaryColumn(employees) + pd.testing.assert_frame_equal(result, expected_output) + + def test_modify_salary_column_single_row(self): + # Input DataFrame with a single row + employees = pd.DataFrame({ + 'name': ['Alice'], + 'salary': [50000] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'name': ['Alice'], + 'salary': [100000] + }) + + # Call the function and assert equality + result = modifySalaryColumn(employees) + pd.testing.assert_frame_equal(result, expected_output) + + def test_modify_salary_column_zero_salary(self): + # Input DataFrame with a zero salary + employees = pd.DataFrame({ + 'name': ['Bob'], + 'salary': [0] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'name': ['Bob'], + 'salary': [0] + }) + + # Call the function and assert equality + result = modifySalaryColumn(employees) + pd.testing.assert_frame_equal(result, expected_output) + + def test_modify_salary_column_negative_salary(self): + # Input DataFrame with a negative salary + employees = pd.DataFrame({ + 'name': ['Charlie'], + 'salary': [-30000] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'name': ['Charlie'], + 'salary': [-60000] + }) + + # Call the function and assert equality + result = modifySalaryColumn(employees) + pd.testing.assert_frame_equal(result, expected_output) + +# Run the tests +if __name__ == '__main__': + unittest.main() diff --git a/src/test/kotlin/g2801_2900/s2885_rename_columns/solution_test.py b/src/test/kotlin/g2801_2900/s2885_rename_columns/solution_test.py new file mode 100644 index 00000000..b1c87065 --- /dev/null +++ b/src/test/kotlin/g2801_2900/s2885_rename_columns/solution_test.py @@ -0,0 +1,88 @@ +import pandas as pd +import unittest + +# The function to be tested +def renameColumns(students: pd.DataFrame) -> pd.DataFrame: + students.rename(columns={'id': 'student_id', 'first': 'first_name', 'last': 'last_name', 'age': 'age_in_years'}, inplace=True) + return students + +# Test class +class TestRenameColumns(unittest.TestCase): + + def test_rename_columns_basic_case(self): + # Input DataFrame + students = pd.DataFrame({ + 'id': [1, 2, 3, 4, 5], + 'first': ['Mason', 'Ava', 'Taylor', 'Georgia', 'Thomas'], + 'last': ['King', 'Wright', 'Hall', 'Thompson', 'Moore'], + 'age': [6, 7, 16, 18, 10] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'student_id': [1, 2, 3, 4, 5], + 'first_name': ['Mason', 'Ava', 'Taylor', 'Georgia', 'Thomas'], + 'last_name': ['King', 'Wright', 'Hall', 'Thompson', 'Moore'], + 'age_in_years': [6, 7, 16, 18, 10] + }) + + # Call the function and assert equality + result = renameColumns(students) + pd.testing.assert_frame_equal(result, expected_output) + + def test_rename_columns_empty_dataframe(self): + # Input: Empty DataFrame with the correct column names + students = pd.DataFrame(columns=['id', 'first', 'last', 'age']) + + # Expected output: Empty DataFrame with renamed columns + expected_output = pd.DataFrame(columns=['student_id', 'first_name', 'last_name', 'age_in_years']) + + # Call the function and assert equality + result = renameColumns(students) + pd.testing.assert_frame_equal(result, expected_output) + + def test_rename_columns_single_row(self): + # Input DataFrame with a single row + students = pd.DataFrame({ + 'id': [10], + 'first': ['Emma'], + 'last': ['Johnson'], + 'age': [15] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'student_id': [10], + 'first_name': ['Emma'], + 'last_name': ['Johnson'], + 'age_in_years': [15] + }) + + # Call the function and assert equality + result = renameColumns(students) + pd.testing.assert_frame_equal(result, expected_output) + + def test_rename_columns_with_different_ages(self): + # Input DataFrame with various ages + students = pd.DataFrame({ + 'id': [101, 102], + 'first': ['Liam', 'Olivia'], + 'last': ['Brown', 'Davis'], + 'age': [21, 30] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'student_id': [101, 102], + 'first_name': ['Liam', 'Olivia'], + 'last_name': ['Brown', 'Davis'], + 'age_in_years': [21, 30] + }) + + # Call the function and assert equality + result = renameColumns(students) + pd.testing.assert_frame_equal(result, expected_output) + +# Run the tests +if __name__ == '__main__': + unittest.main() diff --git a/src/test/kotlin/g2801_2900/s2886_change_data_type/solution_test.py b/src/test/kotlin/g2801_2900/s2886_change_data_type/solution_test.py new file mode 100644 index 00000000..2da19067 --- /dev/null +++ b/src/test/kotlin/g2801_2900/s2886_change_data_type/solution_test.py @@ -0,0 +1,114 @@ +import pandas as pd +import unittest + +# The function to be tested +def changeDatatype(students: pd.DataFrame) -> pd.DataFrame: + students['grade'] = students['grade'].astype('int64') + return students + +# Test class +class TestChangeDatatype(unittest.TestCase): + + def test_change_datatype_basic_case(self): + # Input DataFrame + students = pd.DataFrame({ + 'student_id': [1, 2], + 'name': ['Ava', 'Kate'], + 'age': [6, 15], + 'grade': [73.0, 87.0] + }) + + # Expected output DataFrame with grade explicitly set as int64 + expected_output = pd.DataFrame({ + 'student_id': [1, 2], + 'name': ['Ava', 'Kate'], + 'age': [6, 15], + 'grade': [73, 87] + }) + expected_output['grade'] = expected_output['grade'].astype('int64') + + # Call the function and assert equality + result = changeDatatype(students) + pd.testing.assert_frame_equal(result, expected_output) + + def test_change_datatype_empty_dataframe(self): + # Input: Empty DataFrame with the correct columns + students = pd.DataFrame(columns=['student_id', 'name', 'age', 'grade']) + + # Expected output: Empty DataFrame with the same columns and grade set to Int64 dtype + expected_output = pd.DataFrame(columns=['student_id', 'name', 'age', 'grade']) + expected_output['grade'] = expected_output['grade'].astype('int64') + + # Call the function and assert equality + result = changeDatatype(students) + pd.testing.assert_frame_equal(result, expected_output) + + def test_change_datatype_with_negative_grades(self): + # Input DataFrame with negative grades + students = pd.DataFrame({ + 'student_id': [3, 4], + 'name': ['Liam', 'Olivia'], + 'age': [12, 10], + 'grade': [-45.0, -88.0] + }) + + # Expected output DataFrame with grades as integers + expected_output = pd.DataFrame({ + 'student_id': [3, 4], + 'name': ['Liam', 'Olivia'], + 'age': [12, 10], + 'grade': [-45, -88] + }) + expected_output['grade'] = expected_output['grade'].astype('int64') + + # Call the function and assert equality + result = changeDatatype(students) + pd.testing.assert_frame_equal(result, expected_output) + + def test_change_datatype_with_decimal_grades(self): + # Input DataFrame with decimal grades that will truncate + students = pd.DataFrame({ + 'student_id': [5, 6], + 'name': ['Ella', 'Noah'], + 'age': [14, 17], + 'grade': [95.6, 78.9] + }) + + # Expected output DataFrame with truncated grades as integers + expected_output = pd.DataFrame({ + 'student_id': [5, 6], + 'name': ['Ella', 'Noah'], + 'age': [14, 17], + 'grade': [95, 78] + }) + expected_output['grade'] = expected_output['grade'].astype('int64') + + # Call the function and assert equality + result = changeDatatype(students) + pd.testing.assert_frame_equal(result, expected_output) + + def test_change_datatype_single_row(self): + # Input DataFrame with a single row + students = pd.DataFrame({ + 'student_id': [7], + 'name': ['James'], + 'age': [11], + 'grade': [80.0] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'student_id': [7], + 'name': ['James'], + 'age': [11], + 'grade': [80] + }) + expected_output['grade'] = expected_output['grade'].astype('int64') + + # Call the function and assert equality + result = changeDatatype(students) + pd.testing.assert_frame_equal(result, expected_output) + +# Run the tests +if __name__ == '__main__': + unittest.main() diff --git a/src/test/kotlin/g2801_2900/s2887_fill_missing_data/solution_test.py b/src/test/kotlin/g2801_2900/s2887_fill_missing_data/solution_test.py new file mode 100644 index 00000000..736bab7b --- /dev/null +++ b/src/test/kotlin/g2801_2900/s2887_fill_missing_data/solution_test.py @@ -0,0 +1,92 @@ +import pandas as pd +import unittest + +# The updated function to be tested +def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame: + products['quantity'].fillna(0, inplace=True) + products['quantity'] = products['quantity'].astype(int) # Ensure the quantity is of type int + return products + +# Test class +class TestFillMissingValues(unittest.TestCase): + + def test_fill_missing_values_basic_case(self): + # Input DataFrame with missing values in quantity + products = pd.DataFrame({ + 'name': ['Wristwatch', 'WirelessEarbuds', 'GolfClubs', 'Printer'], + 'quantity': [None, None, 779, 849], + 'price': [135, 821, 9319, 3051] + }) + + # Expected output DataFrame + expected_output = pd.DataFrame({ + 'name': ['Wristwatch', 'WirelessEarbuds', 'GolfClubs', 'Printer'], + 'quantity': [0, 0, 779, 849], + 'price': [135, 821, 9319, 3051] + }) + + # Call the function and assert equality + result = fillMissingValues(products) + pd.testing.assert_frame_equal(result, expected_output) + + def test_fill_missing_values_no_missing(self): + # Input DataFrame with no missing values + products = pd.DataFrame({ + 'name': ['Laptop', 'Mouse', 'Keyboard'], + 'quantity': [10, 5, 0], + 'price': [1000, 50, 30] + }) + + # Expected output should be the same as input + expected_output = products.copy() + + # Call the function and assert equality + result = fillMissingValues(products) + pd.testing.assert_frame_equal(result, expected_output) + + def test_fill_missing_values_empty_dataframe(self): + # Input: Empty DataFrame + products = pd.DataFrame(columns=['name', 'quantity', 'price']) + + # Expected output: Empty DataFrame with specified dtypes + expected_output = pd.DataFrame(columns=['name', 'quantity', 'price'], dtype='object') + expected_output['quantity'] = expected_output['quantity'].astype('int64') + + # Call the function and assert equality + result = fillMissingValues(products) + pd.testing.assert_frame_equal(result, expected_output) + + def test_fill_missing_values_empty_dataframe(self): + # Input: Empty DataFrame + products = pd.DataFrame(columns=['name', 'quantity', 'price']) + + # Expected output: Empty DataFrame + expected_output = pd.DataFrame(columns=['name', 'quantity', 'price']) + expected_output['quantity'] = expected_output['quantity'].astype('int64') + + # Call the function and assert equality + result = fillMissingValues(products) + pd.testing.assert_frame_equal(result, expected_output) + + def test_fill_missing_values_all_none(self): + # Input DataFrame with all None in quantity + products = pd.DataFrame({ + 'name': ['Item1', 'Item2', 'Item3'], + 'quantity': [None, None, None], + 'price': [100, 200, 300] + }) + + # Expected output DataFrame with quantity filled with 0 + expected_output = pd.DataFrame({ + 'name': ['Item1', 'Item2', 'Item3'], + 'quantity': [0, 0, 0], + 'price': [100, 200, 300] + }) + + # Call the function and assert equality + result = fillMissingValues(products) + pd.testing.assert_frame_equal(result, expected_output) + +# Run the tests +if __name__ == '__main__': + unittest.main()