Complete the code to add 7 days to the date in the 'date' column.
import pandas as pd from datetime import timedelta df = pd.DataFrame({'date': pd.to_datetime(['2024-01-01', '2024-01-02'])}) df['new_date'] = df['date'] + [1](days=7) print(df)
We use timedelta to add a specific number of days to a date.
Complete the code to subtract 3 days from the 'date' column.
import pandas as pd from datetime import timedelta df = pd.DataFrame({'date': pd.to_datetime(['2024-03-10', '2024-03-15'])}) df['earlier_date'] = df['date'] - [1](days=3) print(df)
Subtracting a timedelta of 3 days moves the date 3 days earlier.
Fix the error in the code to add 10 days to the 'date' column.
import pandas as pd from datetime import timedelta df = pd.DataFrame({'date': pd.to_datetime(['2024-05-01', '2024-05-02'])}) df['future_date'] = df['date'] + [1](10) print(df)
The timedelta constructor can use named arguments like days=10. Passing just 10 is allowed and sets days=10.
Fill both blanks to create a dictionary with words as keys and their lengths if length is greater than 4.
words = ['apple', 'bat', 'grape', 'kiwi'] lengths = {word: [1] for word in words if [2] print(lengths)
The dictionary comprehension uses len(word) as value and filters words with length greater than 4.
Fill all three blanks to create a dictionary with uppercase words as keys and their lengths if length is greater than 3.
words = ['dog', 'cat', 'elephant', 'fox'] result = { [1]: [2] for word in words if [3] } print(result)
The dictionary comprehension uses the uppercase word as key, length as value, and filters words longer than 3 characters.