Skip to content

Improved tasks 2888, 2889, 2890, 2891 #1881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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()
82 changes: 82 additions & 0 deletions src/test/java/g2801_2900/s2889_reshape_data_pivot/solution_test.py
Original file line number Diff line number Diff line change
@@ -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()
82 changes: 82 additions & 0 deletions src/test/java/g2801_2900/s2890_reshape_data_melt/solution_test.py
Original file line number Diff line number Diff line change
@@ -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()
Loading
Loading