Complete the code to convert the 'date' column to datetime format.
import pandas as pd df = pd.DataFrame({'date': ['2023-01-01', '2023-02-01', '2023-03-01']}) df['date'] = pd.[1](df['date'])
We use pd.to_datetime() to convert strings to datetime objects in pandas.
Complete the code to extract the year from the 'date' column.
df['year'] = df['date'].dt.[1]
The dt.year attribute extracts the year from datetime values.
Fix the error in the code to filter rows where the date is after January 31, 2023.
filtered = df[df['date'] > pd.[1]('2023-01-31')]
We must convert the string '2023-01-31' to datetime to compare with the 'date' column.
Fill both blanks to create a new column 'month_day' with month and day as strings.
df['month_day'] = df['date'].dt.[1].astype(str) + '-' + df['date'].dt.[2].astype(str)
We extract month and day parts and convert them to strings to join with '-'.
Fill all three blanks to create a dictionary with date as key and year as value for dates after 2023-01-15.
result = {row['date']: row['date'].dt.[1] for _, row in df[df['date'] > pd.[2]('2023-01-15')].iterrows() if row['date'].dt.[3] == 2023}We use dt.year to get the year, convert the string to datetime with pd.to_datetime, and check if the year equals 2023.