0
0
Pandasdata~20 mins

Datetime type in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Datetime Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this pandas datetime conversion?
Given the following code, what will be the output of df['date_converted'].dtype?
Pandas
import pandas as pd

df = pd.DataFrame({'date_str': ['2023-01-01', '2023-06-15', '2023-12-31']})
df['date_converted'] = pd.to_datetime(df['date_str'])
print(df['date_converted'].dtype)
Adatetime64[ns]
Bobject
Cstring
Dint64
Attempts:
2 left
💡 Hint
Think about what type pandas uses to store datetime values internally.
data_output
intermediate
2:00remaining
What is the output of extracting the year from datetime?
What will be the output of df['year'] after running this code?
Pandas
import pandas as pd

df = pd.DataFrame({'dates': pd.to_datetime(['2022-05-20', '2023-07-15', '2024-01-01'])})
df['year'] = df['dates'].dt.year
print(df['year'].tolist())
A['2022', '2023', '2024']
B[2022, 2023, 2024]
C[5, 7, 1]
D[20, 15, 1]
Attempts:
2 left
💡 Hint
The dt.year extracts the year as an integer from datetime values.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
This code tries to convert a column to datetime but raises an error. What is the cause?
Pandas
import pandas as pd

df = pd.DataFrame({'dates': ['2023-01-01', 'not_a_date', '2023-03-01']})
df['dates_converted'] = pd.to_datetime(df['dates'])
AValueError because 'not_a_date' cannot be converted to datetime
BTypeError because the column is not a string type
CKeyError because 'dates_converted' column does not exist yet
DSyntaxError due to missing parentheses
Attempts:
2 left
💡 Hint
Check if all strings in the column are valid date formats.
visualization
advanced
3:00remaining
Which plot shows the count of dates by month correctly?
Given this DataFrame, which option produces a bar plot showing counts of dates grouped by month?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'dates': pd.to_datetime(['2023-01-15', '2023-01-20', '2023-02-10', '2023-02-15', '2023-03-01'])})
A
df['month'] = df['dates'].dt.month
counts = df.groupby('month').size()
counts.plot(kind='line')
plt.show()
B
df['month'] = df['dates'].month
counts = df['month'].count()
counts.plot(kind='bar')
plt.show()
C
df['month'] = df['dates'].dt.month
counts = df['month'].value_counts()
counts.plot(kind='pie')
plt.show()
D
df['month'] = df['dates'].dt.month
counts = df['month'].value_counts().sort_index()
counts.plot(kind='bar')
plt.show()
Attempts:
2 left
💡 Hint
Use dt.month and count values grouped by month, then plot a bar chart.
🧠 Conceptual
expert
3:00remaining
What is the effect of timezone conversion on pandas datetime?
If a pandas datetime column is timezone-naive and you convert it to a timezone-aware datetime with dt.tz_localize('UTC'), what happens?
AThe datetime values become strings with timezone info appended
BThe datetime values are shifted to UTC time adjusting the clock time accordingly
CThe datetime values are treated as UTC time without changing the clock time, adding timezone info
DThe datetime values raise an error because timezone-naive cannot be localized
Attempts:
2 left
💡 Hint
Localizing adds timezone info without changing the actual time values.