Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit22455a9

Browse files
authored
Improved tasks 2888, 2889, 2890, 2891
1 parent1891ffc commit22455a9

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
importunittest
2+
importpandasaspd
3+
frompandas.testingimportassert_frame_equal
4+
5+
defconcatenateTables(df1:pd.DataFrame,df2:pd.DataFrame)->pd.DataFrame:
6+
returnpd.concat([df1,df2],ignore_index=True)
7+
8+
classTestConcatenateTables(unittest.TestCase):
9+
deftest_concatenate_normal_case(self):
10+
# Input DataFrames
11+
df1=pd.DataFrame({
12+
"student_id": [1,2,3,4],
13+
"name": ["Mason","Ava","Taylor","Georgia"],
14+
"age": [8,6,15,17]
15+
})
16+
df2=pd.DataFrame({
17+
"student_id": [5,6],
18+
"name": ["Leo","Alex"],
19+
"age": [7,7]
20+
})
21+
22+
# Expected Output
23+
expected=pd.DataFrame({
24+
"student_id": [1,2,3,4,5,6],
25+
"name": ["Mason","Ava","Taylor","Georgia","Leo","Alex"],
26+
"age": [8,6,15,17,7,7]
27+
})
28+
29+
# Actual Output
30+
result=concatenateTables(df1,df2)
31+
32+
# Assert the result matches the expected DataFrame
33+
try:
34+
assert_frame_equal(result,expected)
35+
exceptAssertionErrorase:
36+
self.fail(f"DataFrames are not equal:{e}")
37+
38+
deftest_concatenate_empty_df1(self):
39+
# Input DataFrames
40+
df1=pd.DataFrame(columns=["student_id","name","age"]).astype({
41+
"student_id":"int64",
42+
"name":"object",
43+
"age":"int64"
44+
})
45+
46+
df2=pd.DataFrame({
47+
"student_id": [5,6],
48+
"name": ["Leo","Alex"],
49+
"age": [7,7]
50+
})
51+
52+
# Expected Output
53+
expected=pd.DataFrame({
54+
"student_id": [5,6],
55+
"name": ["Leo","Alex"],
56+
"age": [7,7]
57+
})
58+
59+
# Actual Output
60+
result=concatenateTables(df1,df2)
61+
62+
# Assert the result matches the expected DataFrame
63+
try:
64+
assert_frame_equal(result,expected)
65+
exceptAssertionErrorase:
66+
self.fail(f"DataFrames are not equal when df1 is empty:{e}")
67+
68+
deftest_concatenate_empty_df2(self):
69+
# Input DataFrames
70+
df1=pd.DataFrame({
71+
"student_id": [1,2,3,4],
72+
"name": ["Mason","Ava","Taylor","Georgia"],
73+
"age": [8,6,15,17]
74+
})
75+
df2=pd.DataFrame(columns=["student_id","name","age"]).astype({
76+
"student_id":"int64",
77+
"name":"object",
78+
"age":"int64"
79+
})
80+
81+
# Expected Output
82+
expected=df1
83+
84+
# Actual Output
85+
result=concatenateTables(df1,df2)
86+
87+
# Assert the result matches the expected DataFrame
88+
try:
89+
assert_frame_equal(result,expected)
90+
exceptAssertionErrorase:
91+
self.fail(f"DataFrames are not equal when df2 is empty:{e}")
92+
93+
deftest_concatenate_both_empty(self):
94+
# Input DataFrames
95+
df1=pd.DataFrame(columns=["student_id","name","age"])
96+
df2=pd.DataFrame(columns=["student_id","name","age"])
97+
98+
# Expected Output
99+
expected=pd.DataFrame(columns=["student_id","name","age"])
100+
101+
# Actual Output
102+
result=concatenateTables(df1,df2)
103+
104+
# Assert the result matches the expected DataFrame
105+
try:
106+
assert_frame_equal(result,expected)
107+
exceptAssertionErrorase:
108+
self.fail(f"DataFrames are not equal when both are empty:{e}")
109+
110+
deftest_concatenate_different_column_order(self):
111+
# Input DataFrames
112+
df1=pd.DataFrame({
113+
"student_id": [1,2],
114+
"name": ["Mason","Ava"],
115+
"age": [8,6]
116+
})
117+
df2=pd.DataFrame({
118+
"name": ["Leo","Alex"],
119+
"age": [7,7],
120+
"student_id": [5,6]
121+
})
122+
123+
# Expected Output
124+
expected=pd.DataFrame({
125+
"student_id": [1,2,5,6],
126+
"name": ["Mason","Ava","Leo","Alex"],
127+
"age": [8,6,7,7]
128+
})
129+
130+
# Actual Output
131+
result=concatenateTables(df1,df2)
132+
133+
# Assert the result matches the expected DataFrame
134+
try:
135+
assert_frame_equal(result,expected)
136+
exceptAssertionErrorase:
137+
self.fail(f"DataFrames are not equal when columns are in different orders:{e}")
138+
139+
# Run the tests
140+
if__name__=="__main__":
141+
unittest.main()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
importunittest
2+
importpandasaspd
3+
frompandas.testingimportassert_frame_equal
4+
5+
# Method to be tested
6+
defpivotTable(weather:pd.DataFrame)->pd.DataFrame:
7+
result=weather.pivot(index='month',columns='city',values='temperature')
8+
result.columns.name=None
9+
returnresult.reset_index()
10+
11+
# Unit Test Class
12+
classTestPivotTable(unittest.TestCase):
13+
deftest_pivot_table(self):
14+
# Input DataFrame
15+
input_data= {
16+
"city": ["Jacksonville","Jacksonville","Jacksonville","Jacksonville","Jacksonville",
17+
"ElPaso","ElPaso","ElPaso","ElPaso","ElPaso"],
18+
"month": ["January","February","March","April","May",
19+
"January","February","March","April","May"],
20+
"temperature": [13,23,38,5,34,20,6,26,2,43]
21+
}
22+
weather=pd.DataFrame(input_data)
23+
24+
# Expected Output DataFrame
25+
expected_data= {
26+
"month": ["April","February","January","March","May"],
27+
"ElPaso": [2,6,20,26,43],
28+
"Jacksonville": [5,23,13,38,34]
29+
}
30+
expected_df=pd.DataFrame(expected_data)
31+
32+
# Actual Output
33+
result_df=pivotTable(weather)
34+
35+
# Assert the DataFrames are equal
36+
try:
37+
assert_frame_equal(result_df,expected_df)
38+
exceptAssertionErrorase:
39+
self.fail(f"DataFrames are not equal:{e}")
40+
41+
deftest_empty_dataframe(self):
42+
# Test for an empty input DataFrame
43+
weather=pd.DataFrame(columns=["city","month","temperature"])
44+
expected_df=pd.DataFrame(columns=["month"])
45+
46+
# Actual Output
47+
result_df=pivotTable(weather)
48+
49+
# Assert the DataFrames are equal
50+
try:
51+
assert_frame_equal(result_df,expected_df)
52+
exceptAssertionErrorase:
53+
self.fail(f"DataFrames are not equal for empty input:{e}")
54+
55+
deftest_single_row_dataframe(self):
56+
# Test for a single row input DataFrame
57+
input_data= {
58+
"city": ["ElPaso"],
59+
"month": ["January"],
60+
"temperature": [20]
61+
}
62+
weather=pd.DataFrame(input_data)
63+
64+
# Expected Output DataFrame
65+
expected_data= {
66+
"month": ["January"],
67+
"ElPaso": [20]
68+
}
69+
expected_df=pd.DataFrame(expected_data)
70+
71+
# Actual Output
72+
result_df=pivotTable(weather)
73+
74+
# Assert the DataFrames are equal
75+
try:
76+
assert_frame_equal(result_df,expected_df)
77+
exceptAssertionErrorase:
78+
self.fail(f"DataFrames are not equal for single row input:{e}")
79+
80+
# Run the tests
81+
if__name__=="__main__":
82+
unittest.main()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
importunittest
2+
importpandasaspd
3+
frompandas.testingimportassert_frame_equal
4+
5+
defmeltTable(report:pd.DataFrame)->pd.DataFrame:
6+
returnreport.melt(id_vars='product',var_name='quarter',value_name='sales')
7+
8+
# Unit Test Class
9+
classTestMeltTable(unittest.TestCase):
10+
deftest_melt_table(self):
11+
# Input DataFrame
12+
input_data= {
13+
"product": ["Umbrella","SleepingBag"],
14+
"quarter_1": [417,800],
15+
"quarter_2": [224,936],
16+
"quarter_3": [379,93],
17+
"quarter_4": [611,875]
18+
}
19+
report=pd.DataFrame(input_data)
20+
21+
# Expected Output DataFrame
22+
expected_data= {
23+
"product": ["Umbrella","SleepingBag","Umbrella","SleepingBag","Umbrella","SleepingBag","Umbrella","SleepingBag"],
24+
"quarter": ["quarter_1","quarter_1","quarter_2","quarter_2","quarter_3","quarter_3","quarter_4","quarter_4"],
25+
"sales": [417,800,224,936,379,93,611,875]
26+
}
27+
expected_df=pd.DataFrame(expected_data)
28+
29+
# Actual Output
30+
result_df=meltTable(report)
31+
32+
# Assert DataFrames are equal
33+
try:
34+
assert_frame_equal(result_df,expected_df)
35+
exceptAssertionErrorase:
36+
self.fail(f"DataFrames are not equal:{e}")
37+
38+
deftest_empty_dataframe(self):
39+
# Test with an empty DataFrame
40+
report=pd.DataFrame(columns=["product","quarter_1","quarter_2","quarter_3","quarter_4"])
41+
expected_df=pd.DataFrame(columns=["product","quarter","sales"])
42+
43+
# Actual Output
44+
result_df=meltTable(report)
45+
46+
# Assert DataFrames are equal
47+
try:
48+
assert_frame_equal(result_df,expected_df)
49+
exceptAssertionErrorase:
50+
self.fail(f"DataFrames are not equal for empty input:{e}")
51+
52+
deftest_single_row_dataframe(self):
53+
# Test with a single row DataFrame
54+
input_data= {
55+
"product": ["Umbrella"],
56+
"quarter_1": [417],
57+
"quarter_2": [224],
58+
"quarter_3": [379],
59+
"quarter_4": [611]
60+
}
61+
report=pd.DataFrame(input_data)
62+
63+
# Expected Output DataFrame
64+
expected_data= {
65+
"product": ["Umbrella","Umbrella","Umbrella","Umbrella"],
66+
"quarter": ["quarter_1","quarter_2","quarter_3","quarter_4"],
67+
"sales": [417,224,379,611]
68+
}
69+
expected_df=pd.DataFrame(expected_data)
70+
71+
# Actual Output
72+
result_df=meltTable(report)
73+
74+
# Assert DataFrames are equal
75+
try:
76+
assert_frame_equal(result_df,expected_df)
77+
exceptAssertionErrorase:
78+
self.fail(f"DataFrames are not equal for single row input:{e}")
79+
80+
# Run the tests
81+
if__name__=="__main__":
82+
unittest.main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp