Type hints annotate function parameters and return values with intended data types. They make unfamiliar code easier to read, but they do not enforce types at runtime. section 3.4
Annotating a small function
def add(a: int, b: int) -> int: return a + b The annotations communicate that add expects integers and returns an integer. A separate isinstance check is needed to validate inputs and raise TypeError. section 3.4
Type hints become especially useful when functions are split into data-science pipeline steps. Annotations can identify filenames, pandas data frames and series, numeric thresholds, and predicted outputs. section 3.4.1
Listing 3.31 The code here is a copy of our earlier example in Listing 3.24, except now we add in type hints for each function. This code is also available in the code_modularization_with_type_hints.py file from ch3.
import pandas as pd #1
def read_data(file_name: str) -> pd.DataFrame: #2
df = pd.read_csv(file_name) #2
return df #2
def cap_outliers(feature:pd.Series,
upper_percentile: float, #3
lower_percentile: float) -> pd.Series: #3
# get the lower and upper thresholds #3
# based on the input percentiles #3
lower_thresh = feature.quantile(lower_percentile) #3
upper_thresh = feature.quantile(upper_percentile) #3
# cap any outliers at the lower
# and upper threshold bounds #3
feature = feature.\ #3
map(lambda val: \ #3
lower_thresh if val < lower_thresh \ #3
else upper_thresh if val > upper_thresh \ #3
else val) #3
return feature #3
def clean_data(df: pd.DataFrame,
upper_percentile: float, #4
lower_percentile: float) -> pd.DataFrame: #4
df = df.fillna(df.median(numeric_only=True)) #4
for col in df.columns: #4
df[col] = cap_outliers(df[col], #4
upper_percentile, #4
lower_percentile) #4
# additional cleaning... #4
# [code block] #4
return df #4
if __name__ == "__main__": #5
customer_data = read_data("customer_data.csv") #5
cleaned_data = clean_data(customer_data) #5
# additional code #5
# [code block] #5
Inspect a modular data-cleaning example annotated with str, pandas DataFrame and Series, and float types. section 3.4.1





















