0
0
Pandasdata~20 mins

Extracting day of week and hour in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Datetime Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of extracting day of week and hour?
Given the following pandas DataFrame with a datetime column, what will be the output after extracting the day of week and hour into new columns?
Pandas
import pandas as pd

df = pd.DataFrame({
    'timestamp': pd.to_datetime(['2024-06-01 08:30', '2024-06-02 15:45', '2024-06-03 23:00'])
})
df['day_of_week'] = df['timestamp'].dt.dayofweek
df['hour'] = df['timestamp'].dt.hour
print(df[['day_of_week', 'hour']])
A
   day_of_week  hour
0            0     8
1            1    15
2            2    23
B
   day_of_week  hour
0            6     8
1            0    15
2            1    23
C
   day_of_week  hour
0            4     8
1            5    15
2            6    23
D
   day_of_week  hour
0            5     8
1            6    15
2            0    23
Attempts:
2 left
💡 Hint
Remember that pandas uses Monday=0 for dayofweek and Sunday=6.
data_output
intermediate
1:30remaining
How many unique days of week are in the DataFrame?
Using the DataFrame below, how many unique day_of_week values are present after extraction?
Pandas
import pandas as pd

df = pd.DataFrame({
    'timestamp': pd.to_datetime([
        '2024-06-01 08:30', '2024-06-02 15:45', '2024-06-03 23:00',
        '2024-06-08 10:00', '2024-06-09 12:00'
    ])
})
df['day_of_week'] = df['timestamp'].dt.dayofweek
unique_days = df['day_of_week'].nunique()
print(unique_days)
A3
B5
C7
D2
Attempts:
2 left
💡 Hint
Check the dates and their corresponding weekdays carefully.
🔧 Debug
advanced
1:30remaining
Identify the error in extracting hour from a string column
What error will this code raise when trying to extract the hour from a string column without conversion?
Pandas
import pandas as pd

df = pd.DataFrame({
    'timestamp': ['2024-06-01 08:30', '2024-06-02 15:45']
})
df['hour'] = df['timestamp'].dt.hour
print(df['hour'])
ATypeError: unsupported operand type(s) for -: 'str' and 'int'
BKeyError: 'hour'
CAttributeError: Can only use .dt accessor with datetimelike values
DValueError: invalid literal for int() with base 10
Attempts:
2 left
💡 Hint
The .dt accessor only works on datetime columns, not strings.
🚀 Application
advanced
2:30remaining
Filter DataFrame rows for weekend hours between 18 and 23
Given a DataFrame with a datetime column, which code snippet correctly filters rows where the day is Saturday or Sunday and the hour is between 18 and 23 inclusive?
Pandas
import pandas as pd

df = pd.DataFrame({
    'timestamp': pd.to_datetime([
        '2024-06-01 17:00', '2024-06-01 19:00', '2024-06-02 20:00', '2024-06-03 21:00'
    ])
})
df['day_of_week'] = df['timestamp'].dt.dayofweek
df['hour'] = df['timestamp'].dt.hour
Adf[(df['day_of_week'] in [5,6]) & (df['hour'] >= 18) & (df['hour'] <= 23)]
Bdf[(df['day_of_week'].isin([5,6])) & (df['hour'] >= 18) & (df['hour'] <= 23)]
Cdf[(df['day_of_week'] == 5 & df['day_of_week'] == 6) & (df['hour'] >= 18) & (df['hour'] <= 23)]
Ddf[(df['day_of_week'] == 5 or df['day_of_week'] == 6) & (df['hour'] >= 18) & (df['hour'] <= 23)]
Attempts:
2 left
💡 Hint
Use pandas methods for filtering multiple values in a column.
🧠 Conceptual
expert
1:30remaining
Understanding dayofweek vs weekday_name in pandas
Which statement correctly explains the difference between dt.dayofweek and dt.weekday_name in pandas?
A<code>dt.dayofweek</code> returns an integer (Monday=0), while <code>dt.weekday_name</code> returns the day name as a string (e.g., 'Monday').
B<code>dt.dayofweek</code> returns the day name as a string, while <code>dt.weekday_name</code> returns an integer (Monday=0).
C<code>dt.dayofweek</code> and <code>dt.weekday_name</code> both return integers but with different starting days.
D<code>dt.dayofweek</code> returns the week number, while <code>dt.weekday_name</code> returns the day name.
Attempts:
2 left
💡 Hint
One returns numbers, the other returns names.