0
0
Pandasdata~10 mins

to_datetime() for parsing dates 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_str' column to datetime format using pandas.

Pandas
import pandas as pd

df = pd.DataFrame({'date_str': ['2023-01-01', '2023-02-15', '2023-03-20']})
df['date'] = pd.[1](df['date_str'])
print(df['date'])
Drag options to blanks, or click blank then click option'
Ato_date
Bto_datetime
Cdatetime_parse
Dparse_date
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent function like pd.to_date()
Trying to convert without using pandas functions
2fill in blank
medium

Complete the code to parse dates with a specific format 'dd/mm/yyyy' using to_datetime().

Pandas
import pandas as pd

dates = ['31/12/2023', '01/01/2024', '15/02/2024']
df = pd.DataFrame({'dates': dates})
df['parsed'] = pd.to_datetime(df['dates'], format=[1])
print(df['parsed'])
Drag options to blanks, or click blank then click option'
A'%d/%m/%Y'
B'%m-%d-%Y'
C'yyyy-mm-dd'
D'dd/mm/yyyy'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong format string like 'yyyy-mm-dd'
Not using percent signs (%) in the format string
3fill in blank
hard

Fix the error in the code to correctly parse dates with errors ignored.

Pandas
import pandas as pd

dates = ['2023-01-01', 'not a date', '2023-03-15']
df = pd.DataFrame({'dates': dates})
df['parsed'] = pd.to_datetime(df['dates'], errors=[1])
print(df['parsed'])
Drag options to blanks, or click blank then click option'
A'raise'
B'ignore'
C'coerce'
D'skip'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ignore' which returns original data without conversion
Using 'raise' which throws an error on invalid dates
4fill in blank
hard

Fill both blanks to create a dictionary of dates and their weekday names from a list of date strings.

Pandas
import pandas as pd

dates = ['2023-04-01', '2023-04-02', '2023-04-03']
df = pd.DataFrame({'dates': dates})
df['dates'] = pd.to_datetime(df['dates'])
weekday_dict = dict(zip(df['dates'].dt.[1], df['dates'].dt.[2]()))
print(weekday_dict)
Drag options to blanks, or click blank then click option'
Adate
Bday_name
Cweekday
Dday
Attempts:
3 left
💡 Hint
Common Mistakes
Using date which returns the date, not weekday
Using day which returns day of month, not weekday
5fill in blank
hard

Fill all three blanks to create a filtered DataFrame with dates after '2023-01-31' and add a new column with formatted dates.

Pandas
import pandas as pd

dates = ['2023-01-15', '2023-02-10', '2023-03-05']
df = pd.DataFrame({'dates': dates})
df['dates'] = pd.to_datetime(df['dates'])
filtered = df[df['dates'] [1] pd.Timestamp([2])]
filtered['formatted'] = filtered['dates'].dt.[3]('%Y/%m/%d')
print(filtered)
Drag options to blanks, or click blank then click option'
A>
B'2023-01-31'
Cstrftime
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering
Passing date as a string without quotes
Using a non-existent method instead of strftime