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

Commit 4f82bb4

Browse files
authored
Improved tasks 2884, 2885, 2886, 2887
1 parent f0c39f6 commit 4f82bb4

File tree

4 files changed

+387
-0
lines changed

4 files changed

+387
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import pandas as pd
2+
import unittest
3+
4+
# The function to be tested
5+
def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame:
6+
employees['salary'] = employees['salary'] * 2
7+
return employees
8+
9+
# Test class
10+
class TestDropMissingData(unittest.TestCase):
11+
12+
def test_modify_salary_column_basic_case(self):
13+
# Input DataFrame
14+
employees = pd.DataFrame({
15+
'name': ['Jack', 'Piper', 'Mia', 'Ulysses'],
16+
'salary': [19666, 74754, 62509, 54866]
17+
})
18+
19+
# Expected output DataFrame
20+
expected_output = pd.DataFrame({
21+
'name': ['Jack', 'Piper', 'Mia', 'Ulysses'],
22+
'salary': [39332, 149508, 125018, 109732]
23+
})
24+
25+
# Call the function and assert equality
26+
result = modifySalaryColumn(employees)
27+
pd.testing.assert_frame_equal(result, expected_output)
28+
29+
def test_modify_salary_column_empty_dataframe(self):
30+
# Input: Empty DataFrame
31+
employees = pd.DataFrame(columns=['name', 'salary'])
32+
33+
# Expected output: Empty DataFrame
34+
expected_output = pd.DataFrame(columns=['name', 'salary'])
35+
36+
# Call the function and assert equality
37+
result = modifySalaryColumn(employees)
38+
pd.testing.assert_frame_equal(result, expected_output)
39+
40+
def test_modify_salary_column_single_row(self):
41+
# Input DataFrame with a single row
42+
employees = pd.DataFrame({
43+
'name': ['Alice'],
44+
'salary': [50000]
45+
})
46+
47+
# Expected output DataFrame
48+
expected_output = pd.DataFrame({
49+
'name': ['Alice'],
50+
'salary': [100000]
51+
})
52+
53+
# Call the function and assert equality
54+
result = modifySalaryColumn(employees)
55+
pd.testing.assert_frame_equal(result, expected_output)
56+
57+
def test_modify_salary_column_zero_salary(self):
58+
# Input DataFrame with a zero salary
59+
employees = pd.DataFrame({
60+
'name': ['Bob'],
61+
'salary': [0]
62+
})
63+
64+
# Expected output DataFrame
65+
expected_output = pd.DataFrame({
66+
'name': ['Bob'],
67+
'salary': [0]
68+
})
69+
70+
# Call the function and assert equality
71+
result = modifySalaryColumn(employees)
72+
pd.testing.assert_frame_equal(result, expected_output)
73+
74+
def test_modify_salary_column_negative_salary(self):
75+
# Input DataFrame with a negative salary
76+
employees = pd.DataFrame({
77+
'name': ['Charlie'],
78+
'salary': [-30000]
79+
})
80+
81+
# Expected output DataFrame
82+
expected_output = pd.DataFrame({
83+
'name': ['Charlie'],
84+
'salary': [-60000]
85+
})
86+
87+
# Call the function and assert equality
88+
result = modifySalaryColumn(employees)
89+
pd.testing.assert_frame_equal(result, expected_output)
90+
91+
# Run the tests
92+
if __name__ == '__main__':
93+
unittest.main()
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import pandas as pd
2+
import unittest
3+
4+
# The function to be tested
5+
def renameColumns(students: pd.DataFrame) -> pd.DataFrame:
6+
students.rename(columns={'id': 'student_id', 'first': 'first_name', 'last': 'last_name', 'age': 'age_in_years'}, inplace=True)
7+
return students
8+
9+
# Test class
10+
class TestRenameColumns(unittest.TestCase):
11+
12+
def test_rename_columns_basic_case(self):
13+
# Input DataFrame
14+
students = pd.DataFrame({
15+
'id': [1, 2, 3, 4, 5],
16+
'first': ['Mason', 'Ava', 'Taylor', 'Georgia', 'Thomas'],
17+
'last': ['King', 'Wright', 'Hall', 'Thompson', 'Moore'],
18+
'age': [6, 7, 16, 18, 10]
19+
})
20+
21+
# Expected output DataFrame
22+
expected_output = pd.DataFrame({
23+
'student_id': [1, 2, 3, 4, 5],
24+
'first_name': ['Mason', 'Ava', 'Taylor', 'Georgia', 'Thomas'],
25+
'last_name': ['King', 'Wright', 'Hall', 'Thompson', 'Moore'],
26+
'age_in_years': [6, 7, 16, 18, 10]
27+
})
28+
29+
# Call the function and assert equality
30+
result = renameColumns(students)
31+
pd.testing.assert_frame_equal(result, expected_output)
32+
33+
def test_rename_columns_empty_dataframe(self):
34+
# Input: Empty DataFrame with the correct column names
35+
students = pd.DataFrame(columns=['id', 'first', 'last', 'age'])
36+
37+
# Expected output: Empty DataFrame with renamed columns
38+
expected_output = pd.DataFrame(columns=['student_id', 'first_name', 'last_name', 'age_in_years'])
39+
40+
# Call the function and assert equality
41+
result = renameColumns(students)
42+
pd.testing.assert_frame_equal(result, expected_output)
43+
44+
def test_rename_columns_single_row(self):
45+
# Input DataFrame with a single row
46+
students = pd.DataFrame({
47+
'id': [10],
48+
'first': ['Emma'],
49+
'last': ['Johnson'],
50+
'age': [15]
51+
})
52+
53+
# Expected output DataFrame
54+
expected_output = pd.DataFrame({
55+
'student_id': [10],
56+
'first_name': ['Emma'],
57+
'last_name': ['Johnson'],
58+
'age_in_years': [15]
59+
})
60+
61+
# Call the function and assert equality
62+
result = renameColumns(students)
63+
pd.testing.assert_frame_equal(result, expected_output)
64+
65+
def test_rename_columns_with_different_ages(self):
66+
# Input DataFrame with various ages
67+
students = pd.DataFrame({
68+
'id': [101, 102],
69+
'first': ['Liam', 'Olivia'],
70+
'last': ['Brown', 'Davis'],
71+
'age': [21, 30]
72+
})
73+
74+
# Expected output DataFrame
75+
expected_output = pd.DataFrame({
76+
'student_id': [101, 102],
77+
'first_name': ['Liam', 'Olivia'],
78+
'last_name': ['Brown', 'Davis'],
79+
'age_in_years': [21, 30]
80+
})
81+
82+
# Call the function and assert equality
83+
result = renameColumns(students)
84+
pd.testing.assert_frame_equal(result, expected_output)
85+
86+
# Run the tests
87+
if __name__ == '__main__':
88+
unittest.main()
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import pandas as pd
2+
import unittest
3+
4+
# The function to be tested
5+
def changeDatatype(students: pd.DataFrame) -> pd.DataFrame:
6+
students['grade'] = students['grade'].astype('int64')
7+
return students
8+
9+
# Test class
10+
class TestChangeDatatype(unittest.TestCase):
11+
12+
def test_change_datatype_basic_case(self):
13+
# Input DataFrame
14+
students = pd.DataFrame({
15+
'student_id': [1, 2],
16+
'name': ['Ava', 'Kate'],
17+
'age': [6, 15],
18+
'grade': [73.0, 87.0]
19+
})
20+
21+
# Expected output DataFrame with grade explicitly set as int64
22+
expected_output = pd.DataFrame({
23+
'student_id': [1, 2],
24+
'name': ['Ava', 'Kate'],
25+
'age': [6, 15],
26+
'grade': [73, 87]
27+
})
28+
expected_output['grade'] = expected_output['grade'].astype('int64')
29+
30+
# Call the function and assert equality
31+
result = changeDatatype(students)
32+
pd.testing.assert_frame_equal(result, expected_output)
33+
34+
def test_change_datatype_empty_dataframe(self):
35+
# Input: Empty DataFrame with the correct columns
36+
students = pd.DataFrame(columns=['student_id', 'name', 'age', 'grade'])
37+
38+
# Expected output: Empty DataFrame with the same columns and grade set to Int64 dtype
39+
expected_output = pd.DataFrame(columns=['student_id', 'name', 'age', 'grade'])
40+
expected_output['grade'] = expected_output['grade'].astype('int64')
41+
42+
# Call the function and assert equality
43+
result = changeDatatype(students)
44+
pd.testing.assert_frame_equal(result, expected_output)
45+
46+
def test_change_datatype_with_negative_grades(self):
47+
# Input DataFrame with negative grades
48+
students = pd.DataFrame({
49+
'student_id': [3, 4],
50+
'name': ['Liam', 'Olivia'],
51+
'age': [12, 10],
52+
'grade': [-45.0, -88.0]
53+
})
54+
55+
# Expected output DataFrame with grades as integers
56+
expected_output = pd.DataFrame({
57+
'student_id': [3, 4],
58+
'name': ['Liam', 'Olivia'],
59+
'age': [12, 10],
60+
'grade': [-45, -88]
61+
})
62+
expected_output['grade'] = expected_output['grade'].astype('int64')
63+
64+
# Call the function and assert equality
65+
result = changeDatatype(students)
66+
pd.testing.assert_frame_equal(result, expected_output)
67+
68+
def test_change_datatype_with_decimal_grades(self):
69+
# Input DataFrame with decimal grades that will truncate
70+
students = pd.DataFrame({
71+
'student_id': [5, 6],
72+
'name': ['Ella', 'Noah'],
73+
'age': [14, 17],
74+
'grade': [95.6, 78.9]
75+
})
76+
77+
# Expected output DataFrame with truncated grades as integers
78+
expected_output = pd.DataFrame({
79+
'student_id': [5, 6],
80+
'name': ['Ella', 'Noah'],
81+
'age': [14, 17],
82+
'grade': [95, 78]
83+
})
84+
expected_output['grade'] = expected_output['grade'].astype('int64')
85+
86+
# Call the function and assert equality
87+
result = changeDatatype(students)
88+
pd.testing.assert_frame_equal(result, expected_output)
89+
90+
def test_change_datatype_single_row(self):
91+
# Input DataFrame with a single row
92+
students = pd.DataFrame({
93+
'student_id': [7],
94+
'name': ['James'],
95+
'age': [11],
96+
'grade': [80.0]
97+
})
98+
99+
# Expected output DataFrame
100+
expected_output = pd.DataFrame({
101+
'student_id': [7],
102+
'name': ['James'],
103+
'age': [11],
104+
'grade': [80]
105+
})
106+
expected_output['grade'] = expected_output['grade'].astype('int64')
107+
108+
# Call the function and assert equality
109+
result = changeDatatype(students)
110+
pd.testing.assert_frame_equal(result, expected_output)
111+
112+
# Run the tests
113+
if __name__ == '__main__':
114+
unittest.main()
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import pandas as pd
2+
import unittest
3+
4+
# The updated function to be tested
5+
def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame:
6+
products['quantity'].fillna(0, inplace=True)
7+
products['quantity'] = products['quantity'].astype(int) # Ensure the quantity is of type int
8+
return products
9+
10+
# Test class
11+
class TestFillMissingValues(unittest.TestCase):
12+
13+
def test_fill_missing_values_basic_case(self):
14+
# Input DataFrame with missing values in quantity
15+
products = pd.DataFrame({
16+
'name': ['Wristwatch', 'WirelessEarbuds', 'GolfClubs', 'Printer'],
17+
'quantity': [None, None, 779, 849],
18+
'price': [135, 821, 9319, 3051]
19+
})
20+
21+
# Expected output DataFrame
22+
expected_output = pd.DataFrame({
23+
'name': ['Wristwatch', 'WirelessEarbuds', 'GolfClubs', 'Printer'],
24+
'quantity': [0, 0, 779, 849],
25+
'price': [135, 821, 9319, 3051]
26+
})
27+
28+
# Call the function and assert equality
29+
result = fillMissingValues(products)
30+
pd.testing.assert_frame_equal(result, expected_output)
31+
32+
def test_fill_missing_values_no_missing(self):
33+
# Input DataFrame with no missing values
34+
products = pd.DataFrame({
35+
'name': ['Laptop', 'Mouse', 'Keyboard'],
36+
'quantity': [10, 5, 0],
37+
'price': [1000, 50, 30]
38+
})
39+
40+
# Expected output should be the same as input
41+
expected_output = products.copy()
42+
43+
# Call the function and assert equality
44+
result = fillMissingValues(products)
45+
pd.testing.assert_frame_equal(result, expected_output)
46+
47+
def test_fill_missing_values_empty_dataframe(self):
48+
# Input: Empty DataFrame
49+
products = pd.DataFrame(columns=['name', 'quantity', 'price'])
50+
51+
# Expected output: Empty DataFrame with specified dtypes
52+
expected_output = pd.DataFrame(columns=['name', 'quantity', 'price'], dtype='object')
53+
expected_output['quantity'] = expected_output['quantity'].astype('int64')
54+
55+
# Call the function and assert equality
56+
result = fillMissingValues(products)
57+
pd.testing.assert_frame_equal(result, expected_output)
58+
59+
def test_fill_missing_values_empty_dataframe(self):
60+
# Input: Empty DataFrame
61+
products = pd.DataFrame(columns=['name', 'quantity', 'price'])
62+
63+
# Expected output: Empty DataFrame
64+
expected_output = pd.DataFrame(columns=['name', 'quantity', 'price'])
65+
expected_output['quantity'] = expected_output['quantity'].astype('int64')
66+
67+
# Call the function and assert equality
68+
result = fillMissingValues(products)
69+
pd.testing.assert_frame_equal(result, expected_output)
70+
71+
def test_fill_missing_values_all_none(self):
72+
# Input DataFrame with all None in quantity
73+
products = pd.DataFrame({
74+
'name': ['Item1', 'Item2', 'Item3'],
75+
'quantity': [None, None, None],
76+
'price': [100, 200, 300]
77+
})
78+
79+
# Expected output DataFrame with quantity filled with 0
80+
expected_output = pd.DataFrame({
81+
'name': ['Item1', 'Item2', 'Item3'],
82+
'quantity': [0, 0, 0],
83+
'price': [100, 200, 300]
84+
})
85+
86+
# Call the function and assert equality
87+
result = fillMissingValues(products)
88+
pd.testing.assert_frame_equal(result, expected_output)
89+
90+
# Run the tests
91+
if __name__ == '__main__':
92+
unittest.main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /