Complete the code to convert the 'date' column to datetime type using pandas.
import pandas as pd df = pd.DataFrame({'date': ['2023-01-01', '2023-02-01', '2023-03-01']}) df['date'] = pd.[1](df['date']) print(df.dtypes)
Use pd.to_datetime() to convert a column to datetime type in pandas.
Complete the code to extract the year from the datetime column 'date' in the DataFrame.
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-01-01', '2024-02-01', '2025-03-01'])}) df['year'] = df['date'].dt.[1] print(df[['date', 'year']])
Use .dt.year to get the year from a datetime column.
Fix the error in the code to filter rows where the 'date' is after January 1, 2024.
import pandas as pd df = pd.DataFrame({'date': pd.to_datetime(['2023-12-31', '2024-01-02', '2024-06-01'])}) filtered = df[df['date'] [1] pd.Timestamp('2024-01-01')] print(filtered)
Use the greater than operator > to filter dates after January 1, 2024.
Fill both blanks to create a dictionary comprehension that maps each date string to its month number if the day is greater than 15.
dates = ['2023-01-10', '2023-02-20', '2023-03-25'] months = {d: pd.to_datetime(d).dt.[1] for d in dates if pd.to_datetime(d).dt.[2] > 15} print(months)
Use .dt.month to get the month and .dt.day to check the day.
Fill all three blanks to create a dictionary comprehension that maps the uppercase month name to the day number for dates in 2023 where the day is less than 20.
dates = ['2023-01-10', '2023-02-25', '2023-03-15', '2024-04-10'] result = {pd.to_datetime(d).dt.[1]().upper(): pd.to_datetime(d).dt.[2] for d in dates if pd.to_datetime(d).dt.[3] == 2023 and pd.to_datetime(d).dt.day < 20} print(result)
Use .dt.month_name() for the month name, .dt.day for the day, and .dt.year to filter the year.