diff --git a/src/test/kotlin/g2801_2900/s2888_reshape_data_concatenate/solution_test.py b/src/test/kotlin/g2801_2900/s2888_reshape_data_concatenate/solution_test.py new file mode 100644 index 00000000..e43c83eb --- /dev/null +++ b/src/test/kotlin/g2801_2900/s2888_reshape_data_concatenate/solution_test.py @@ -0,0 +1,141 @@ +import unittest +import pandas as pd +from pandas.testing import assert_frame_equal + +def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame: + return pd.concat([df1, df2], ignore_index=True) + +class TestConcatenateTables(unittest.TestCase): + def test_concatenate_normal_case(self): + # Input DataFrames + df1 = pd.DataFrame({ + "student_id": [1, 2, 3, 4], + "name": ["Mason", "Ava", "Taylor", "Georgia"], + "age": [8, 6, 15, 17] + }) + df2 = pd.DataFrame({ + "student_id": [5, 6], + "name": ["Leo", "Alex"], + "age": [7, 7] + }) + + # Expected Output + expected = pd.DataFrame({ + "student_id": [1, 2, 3, 4, 5, 6], + "name": ["Mason", "Ava", "Taylor", "Georgia", "Leo", "Alex"], + "age": [8, 6, 15, 17, 7, 7] + }) + + # Actual Output + result = concatenateTables(df1, df2) + + # Assert the result matches the expected DataFrame + try: + assert_frame_equal(result, expected) + except AssertionError as e: + self.fail(f"DataFrames are not equal: {e}") + + def test_concatenate_empty_df1(self): + # Input DataFrames + df1 = pd.DataFrame(columns=["student_id", "name", "age"]).astype({ + "student_id": "int64", + "name": "object", + "age": "int64" + }) + + df2 = pd.DataFrame({ + "student_id": [5, 6], + "name": ["Leo", "Alex"], + "age": [7, 7] + }) + + # Expected Output + expected = pd.DataFrame({ + "student_id": [5, 6], + "name": ["Leo", "Alex"], + "age": [7, 7] + }) + + # Actual Output + result = concatenateTables(df1, df2) + + # Assert the result matches the expected DataFrame + try: + assert_frame_equal(result, expected) + except AssertionError as e: + self.fail(f"DataFrames are not equal when df1 is empty: {e}") + + def test_concatenate_empty_df2(self): + # Input DataFrames + df1 = pd.DataFrame({ + "student_id": [1, 2, 3, 4], + "name": ["Mason", "Ava", "Taylor", "Georgia"], + "age": [8, 6, 15, 17] + }) + df2 = pd.DataFrame(columns=["student_id", "name", "age"]).astype({ + "student_id": "int64", + "name": "object", + "age": "int64" + }) + + # Expected Output + expected = df1 + + # Actual Output + result = concatenateTables(df1, df2) + + # Assert the result matches the expected DataFrame + try: + assert_frame_equal(result, expected) + except AssertionError as e: + self.fail(f"DataFrames are not equal when df2 is empty: {e}") + + def test_concatenate_both_empty(self): + # Input DataFrames + df1 = pd.DataFrame(columns=["student_id", "name", "age"]) + df2 = pd.DataFrame(columns=["student_id", "name", "age"]) + + # Expected Output + expected = pd.DataFrame(columns=["student_id", "name", "age"]) + + # Actual Output + result = concatenateTables(df1, df2) + + # Assert the result matches the expected DataFrame + try: + assert_frame_equal(result, expected) + except AssertionError as e: + self.fail(f"DataFrames are not equal when both are empty: {e}") + + def test_concatenate_different_column_order(self): + # Input DataFrames + df1 = pd.DataFrame({ + "student_id": [1, 2], + "name": ["Mason", "Ava"], + "age": [8, 6] + }) + df2 = pd.DataFrame({ + "name": ["Leo", "Alex"], + "age": [7, 7], + "student_id": [5, 6] + }) + + # Expected Output + expected = pd.DataFrame({ + "student_id": [1, 2, 5, 6], + "name": ["Mason", "Ava", "Leo", "Alex"], + "age": [8, 6, 7, 7] + }) + + # Actual Output + result = concatenateTables(df1, df2) + + # Assert the result matches the expected DataFrame + try: + assert_frame_equal(result, expected) + except AssertionError as e: + self.fail(f"DataFrames are not equal when columns are in different orders: {e}") + +# Run the tests +if __name__ == "__main__": + unittest.main() diff --git a/src/test/kotlin/g2801_2900/s2889_reshape_data_pivot/solution_test.py b/src/test/kotlin/g2801_2900/s2889_reshape_data_pivot/solution_test.py new file mode 100644 index 00000000..c9a510ca --- /dev/null +++ b/src/test/kotlin/g2801_2900/s2889_reshape_data_pivot/solution_test.py @@ -0,0 +1,82 @@ +import unittest +import pandas as pd +from pandas.testing import assert_frame_equal + +# Method to be tested +def pivotTable(weather: pd.DataFrame) -> pd.DataFrame: + result = weather.pivot(index='month', columns='city', values='temperature') + result.columns.name = None + return result.reset_index() + +# Unit Test Class +class TestPivotTable(unittest.TestCase): + def test_pivot_table(self): + # Input DataFrame + input_data = { + "city": ["Jacksonville", "Jacksonville", "Jacksonville", "Jacksonville", "Jacksonville", + "ElPaso", "ElPaso", "ElPaso", "ElPaso", "ElPaso"], + "month": ["January", "February", "March", "April", "May", + "January", "February", "March", "April", "May"], + "temperature": [13, 23, 38, 5, 34, 20, 6, 26, 2, 43] + } + weather = pd.DataFrame(input_data) + + # Expected Output DataFrame + expected_data = { + "month": ["April", "February", "January", "March", "May"], + "ElPaso": [2, 6, 20, 26, 43], + "Jacksonville": [5, 23, 13, 38, 34] + } + expected_df = pd.DataFrame(expected_data) + + # Actual Output + result_df = pivotTable(weather) + + # Assert the DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal: {e}") + + def test_empty_dataframe(self): + # Test for an empty input DataFrame + weather = pd.DataFrame(columns=["city", "month", "temperature"]) + expected_df = pd.DataFrame(columns=["month"]) + + # Actual Output + result_df = pivotTable(weather) + + # Assert the DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal for empty input: {e}") + + def test_single_row_dataframe(self): + # Test for a single row input DataFrame + input_data = { + "city": ["ElPaso"], + "month": ["January"], + "temperature": [20] + } + weather = pd.DataFrame(input_data) + + # Expected Output DataFrame + expected_data = { + "month": ["January"], + "ElPaso": [20] + } + expected_df = pd.DataFrame(expected_data) + + # Actual Output + result_df = pivotTable(weather) + + # Assert the DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal for single row input: {e}") + +# Run the tests +if __name__ == "__main__": + unittest.main() diff --git a/src/test/kotlin/g2801_2900/s2890_reshape_data_melt/solution_test.py b/src/test/kotlin/g2801_2900/s2890_reshape_data_melt/solution_test.py new file mode 100644 index 00000000..103d0e5f --- /dev/null +++ b/src/test/kotlin/g2801_2900/s2890_reshape_data_melt/solution_test.py @@ -0,0 +1,82 @@ +import unittest +import pandas as pd +from pandas.testing import assert_frame_equal + +def meltTable(report: pd.DataFrame) -> pd.DataFrame: + return report.melt(id_vars='product', var_name='quarter', value_name='sales') + +# Unit Test Class +class TestMeltTable(unittest.TestCase): + def test_melt_table(self): + # Input DataFrame + input_data = { + "product": ["Umbrella", "SleepingBag"], + "quarter_1": [417, 800], + "quarter_2": [224, 936], + "quarter_3": [379, 93], + "quarter_4": [611, 875] + } + report = pd.DataFrame(input_data) + + # Expected Output DataFrame + expected_data = { + "product": ["Umbrella", "SleepingBag", "Umbrella", "SleepingBag", "Umbrella", "SleepingBag", "Umbrella", "SleepingBag"], + "quarter": ["quarter_1", "quarter_1", "quarter_2", "quarter_2", "quarter_3", "quarter_3", "quarter_4", "quarter_4"], + "sales": [417, 800, 224, 936, 379, 93, 611, 875] + } + expected_df = pd.DataFrame(expected_data) + + # Actual Output + result_df = meltTable(report) + + # Assert DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal: {e}") + + def test_empty_dataframe(self): + # Test with an empty DataFrame + report = pd.DataFrame(columns=["product", "quarter_1", "quarter_2", "quarter_3", "quarter_4"]) + expected_df = pd.DataFrame(columns=["product", "quarter", "sales"]) + + # Actual Output + result_df = meltTable(report) + + # Assert DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal for empty input: {e}") + + def test_single_row_dataframe(self): + # Test with a single row DataFrame + input_data = { + "product": ["Umbrella"], + "quarter_1": [417], + "quarter_2": [224], + "quarter_3": [379], + "quarter_4": [611] + } + report = pd.DataFrame(input_data) + + # Expected Output DataFrame + expected_data = { + "product": ["Umbrella", "Umbrella", "Umbrella", "Umbrella"], + "quarter": ["quarter_1", "quarter_2", "quarter_3", "quarter_4"], + "sales": [417, 224, 379, 611] + } + expected_df = pd.DataFrame(expected_data) + + # Actual Output + result_df = meltTable(report) + + # Assert DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal for single row input: {e}") + +# Run the tests +if __name__ == "__main__": + unittest.main() diff --git a/src/test/kotlin/g2801_2900/s2891_method_chaining/solution_test.py b/src/test/kotlin/g2801_2900/s2891_method_chaining/solution_test.py new file mode 100644 index 00000000..da5c73d0 --- /dev/null +++ b/src/test/kotlin/g2801_2900/s2891_method_chaining/solution_test.py @@ -0,0 +1,105 @@ +import unittest +import pandas as pd +from pandas.testing import assert_frame_equal + +def findHeavyAnimals(animals: pd.DataFrame) -> pd.DataFrame: + animal_data = {} + for index in animals.index: + animal = animals.iloc[index] + if animal['weight'] > 100: + animal_data[animal['name']] = animal['weight'] + + animal_data = dict(sorted(animal_data.items() , key = lambda x : x[1] , reverse = True)) + result = pd.DataFrame(animal_data.keys() , columns = ['name']) + return result + +class TestFindHeavyAnimals(unittest.TestCase): + def test_find_heavy_animals(self): + # Input DataFrame + animals_data = { + "name": ["Tatiana", "Khaled", "Alex", "Jonathan", "Stefan", "Tommy"], + "species": ["Snake", "Giraffe", "Leopard", "Monkey", "Bear", "Panda"], + "age": [98, 50, 6, 45, 100, 26], + "weight": [464, 41, 328, 463, 50, 349] + } + animals = pd.DataFrame(animals_data) + + # Expected Output DataFrame + expected_data = { + "name": ["Tatiana", "Jonathan", "Tommy", "Alex"] + } + expected_df = pd.DataFrame(expected_data) + + # Actual Output + result_df = findHeavyAnimals(animals) + + # Assert DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal: {e}") + + def test_no_heavy_animals(self): + # Input DataFrame with no animals weighing more than 100 + animals_data = { + "name": ["Khaled", "Stefan"], + "species": ["Giraffe", "Bear"], + "age": [50, 100], + "weight": [41, 50] + } + animals = pd.DataFrame(animals_data) + + # Expected Output: Empty DataFrame + expected_df = pd.DataFrame(columns=["name"]) + + # Actual Output + result_df = findHeavyAnimals(animals) + + # Assert DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal for no heavy animals: {e}") + + def test_single_heavy_animal(self): + # Input DataFrame with one animal weighing more than 100 + animals_data = { + "name": ["Khaled", "Stefan", "Tatiana"], + "species": ["Giraffe", "Bear", "Snake"], + "age": [50, 100, 98], + "weight": [41, 50, 464] + } + animals = pd.DataFrame(animals_data) + + # Expected Output DataFrame + expected_data = { + "name": ["Tatiana"] + } + expected_df = pd.DataFrame(expected_data) + + # Actual Output + result_df = findHeavyAnimals(animals) + + # Assert DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal for single heavy animal: {e}") + + def test_empty_dataframe(self): + # Test with an empty DataFrame + animals = pd.DataFrame(columns=["name", "species", "age", "weight"]) + expected_df = pd.DataFrame(columns=["name"]) + + # Actual Output + result_df = findHeavyAnimals(animals) + + # Assert DataFrames are equal + try: + assert_frame_equal(result_df, expected_df) + except AssertionError as e: + self.fail(f"DataFrames are not equal for empty input: {e}") + +# Run the tests +if __name__ == "__main__": + unittest.main()