Skip to content

Commit 087c2ca

Browse files
authored
Merge branch 'master' into type-hint-arrow-samples
2 parents 367dd36 + df6b8e1 commit 087c2ca

File tree

134 files changed

+2234
-44
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+2234
-44
lines changed

debug-python-errors/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Debug Common Python Errors
2+
3+
This folder provides the code examples for the Real Python tutorial [How to Debug Common Python Errors](https://realpython.com/debug-python-errors/).

debug-python-errors/cat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cat = "Siamese"
2+
3+
print(cat)

debug-python-errors/fruit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def capitalize_fruit_names(fruits):
2+
capitalized_fruit_names = []
3+
cleaned = [fruit if isinstance(fruit, str) else "" for fruit in fruits]
4+
5+
for fruit in cleaned:
6+
capitalized_fruit_names.append(fruit.capitalize())
7+
8+
return capitalized_fruit_names
9+
10+
11+
if __name__ == "__main__":
12+
print(capitalize_fruit_names(["apple", "BANANA", "cherry", "maNgo"]))

debug-python-errors/palindromes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def find_palindromes(text):
2+
# Split sentence into words
3+
words = text.split()
4+
5+
# Remove punctuation and convert to lowercase
6+
normalized_words = [
7+
"".join(filter(str.isalnum, word)).lower() for word in words
8+
]
9+
10+
# Check for palindromes
11+
return [word for word in normalized_words if word == word[::-1]]
12+
13+
14+
if __name__ == "__main__":
15+
print(
16+
find_palindromes("Dad plays many solos at noon, and sees a racecar.")
17+
)

debug-python-errors/test_fruit.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import unittest
2+
3+
from fruit import capitalize_fruit_names
4+
5+
6+
class TestFruit(unittest.TestCase):
7+
def test_empty_list(self):
8+
"""with empty list"""
9+
self.assertEqual(capitalize_fruit_names([]), [])
10+
11+
def test_lowercase(self):
12+
"""with lowercase strings"""
13+
self.assertEqual(
14+
capitalize_fruit_names(["apple", "banana", "cherry"]),
15+
["Apple", "Banana", "Cherry"],
16+
)
17+
18+
def test_uppercase(self):
19+
"""with uppercase strings"""
20+
self.assertEqual(
21+
capitalize_fruit_names(["APPLE", "BANANA", "CHERRY"]),
22+
["Apple", "Banana", "Cherry"],
23+
)
24+
25+
def test_mixed_case(self):
26+
"""with mixed case strings"""
27+
self.assertEqual(
28+
capitalize_fruit_names(["mAnGo", "grApE"]),
29+
["Mango", "Grape"],
30+
)
31+
32+
def test_non_string_element(self):
33+
"""with a mix of integer and string elements"""
34+
self.assertEqual(
35+
capitalize_fruit_names([123, "banana"]),
36+
["", "Banana"],
37+
)
38+
39+
40+
if __name__ == "__main__":
41+
unittest.main()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# How to Drop Null Values in pandas
2+
3+
The materials contained in this download are designed to complement the Real Python tutorial [How to Drop Null Values in pandas](https://realpython.com/how-to-drop-null-values-in-pandas/).
4+
5+
Consider creating a [Python virtual environment](https://realpython.com/python-virtual-environments-a-primer/) before installing the dependencies:
6+
7+
```shell
8+
$ python3 -m venv .venv/ --prompt pandas-nulls
9+
$ source .venv/bin/activate
10+
(pandas-nulls) $ python -m pip install -r requirements.txt
11+
```
12+
13+
Your download bundle contains the following four files:
14+
15+
1. `drop_null_rows.py`
16+
2. `drop_null_columns.py`
17+
3. `drop_a_subset.py`
18+
4. `exercise_solutions.py`
19+
20+
The first three files contain the code from different tutorial sections, while the fourth contains the solutions to the exercise.
21+
22+
There are also two data files containing the data used throughout the tutorial:
23+
24+
1. `sales_data_with_missing_values.csv`
25+
2. `grades.csv`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pandas as pd
2+
3+
pd.set_option("display.max_columns", None)
4+
5+
sales_data = pd.read_csv(
6+
"sales_data_with_missing_values.csv",
7+
parse_dates=["order_date"],
8+
date_format="%d/%m/%Y",
9+
).convert_dtypes(dtype_backend="pyarrow")
10+
11+
print(sales_data.dropna(axis=0, subset=(["discount", "sale_price"])))
12+
print(sales_data.dropna(how="all"))
13+
print(sales_data.dropna(thresh=5))
14+
print(sales_data.dropna(thresh=5, ignore_index=True))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pandas as pd
2+
3+
sales_data = pd.read_csv(
4+
"sales_data_with_missing_values.csv",
5+
parse_dates=["order_date"],
6+
date_format="%d/%m/%Y",
7+
).convert_dtypes(dtype_backend="pyarrow")
8+
9+
print(sales_data.dropna(axis="columns"))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pandas as pd
2+
3+
pd.set_option("display.max_columns", None)
4+
5+
sales_data = pd.read_csv(
6+
"sales_data_with_missing_values.csv",
7+
parse_dates=["order_date"],
8+
date_format="%d/%m/%Y",
9+
).convert_dtypes(dtype_backend="pyarrow")
10+
11+
print(sales_data)
12+
print(sales_data.isna().sum())
13+
print(sales_data.dropna())
14+
15+
clean_sales_data = sales_data.dropna()
16+
print(clean_sales_data)
17+
18+
sales_data.dropna(inplace=True)
19+
print(sales_data)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pandas as pd
2+
3+
grades = pd.read_csv(
4+
"grades.csv",
5+
).convert_dtypes(dtype_backend="pyarrow")
6+
7+
# 1. Use `.dropna()` in such a way that it permanently drops the row in the dataframe containing only null values.
8+
grades.dropna(how="all", inplace=True)
9+
10+
# 2. Display the rows for the exams that all students have completed.
11+
grades.dropna()
12+
13+
# 3. Display any columns with no missing data.
14+
grades.dropna(axis=1)
15+
16+
# 4. Display the exams sat by at least five students.
17+
grades.dropna(axis=0, thresh=6) # Remember there are seven columns.
18+
19+
# 5. Who else was in in every exam that both S2 and S4 sat?
20+
grades.dropna(subset=["S2", "S4"]).dropna(axis=1, ignore_index=True)

0 commit comments

Comments
 (0)