0
0
Pandasdata~10 mins

Datetime type in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the 'date' column to datetime type using pandas.

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)
Drag options to blanks, or click blank then click option'
ATimestamp
Bdatetime
Cto_datetime
Ddate_range
Attempts:
3 left
💡 Hint
Common Mistakes
Using pd.datetime which is not a function.
Using pd.Timestamp which creates a single timestamp, not conversion.
Using pd.date_range which creates a range of dates.
2fill in blank
medium

Complete the code to extract the year from the datetime column 'date' in the DataFrame.

Pandas
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']])
Drag options to blanks, or click blank then click option'
Ayear
Bhour
Cday
Dmonth
Attempts:
3 left
💡 Hint
Common Mistakes
Using .dt.month or .dt.day which extract other parts of the date.
Trying to use a method instead of an attribute.
3fill in blank
hard

Fix the error in the code to filter rows where the 'date' is after January 1, 2024.

Pandas
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)
Drag options to blanks, or click blank then click option'
A<
B<=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or <= which select earlier dates.
Using == which selects only that exact date.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each date string to its month number if the day is greater than 15.

Pandas
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)
Drag options to blanks, or click blank then click option'
Amonth
Bday
Cyear
Dhour
Attempts:
3 left
💡 Hint
Common Mistakes
Using .dt.year instead of .dt.month for the first blank.
Using .dt.month instead of .dt.day for the second blank.
5fill in blank
hard

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.

Pandas
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)
Drag options to blanks, or click blank then click option'
Amonth_name
Bday
Cyear
Dmonth
Attempts:
3 left
💡 Hint
Common Mistakes
Using .dt.month instead of .dt.month_name for the first blank.
Using .dt.month instead of .dt.day for the second blank.
Using .dt.day instead of .dt.year for the third blank.