0
0
Pandasdata~20 mins

Why datetime handling matters in Pandas - Challenge Your Understanding

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
1:30remaining
What is the output of this datetime conversion?
Consider this pandas code that converts a string column to datetime. What will be the output of the df['date'].dtype?
Pandas
import pandas as pd

df = pd.DataFrame({'date': ['2023-01-01', '2023-02-15', '2023-03-30']})
df['date'] = pd.to_datetime(df['date'])
print(df['date'].dtype)
Aobject
Bdatetime64[ns]
Cstring
Dint64
Attempts:
2 left
💡 Hint
Think about what pandas does when converting strings to datetime format.
data_output
intermediate
1:30remaining
How many rows are in this filtered datetime DataFrame?
Given this DataFrame with datetime values, how many rows remain after filtering for dates in March 2023?
Pandas
import pandas as pd

df = pd.DataFrame({'date': pd.to_datetime(['2023-01-10', '2023-03-05', '2023-03-20', '2023-04-01'])})
filtered = df[(df['date'] >= '2023-03-01') & (df['date'] < '2023-04-01')]
print(len(filtered))
A1
B4
C3
D2
Attempts:
2 left
💡 Hint
Count only dates that are in March 2023.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error when subtracting dates?
This code tries to subtract two date columns but raises an error. What is the cause?
Pandas
import pandas as pd

df = pd.DataFrame({'start': ['2023-01-01', '2023-02-01'], 'end': ['2023-01-10', '2023-02-15']})
df['duration'] = df['end'] - df['start']
print(df['duration'])
ATypeError because columns are strings, not datetime objects
BKeyError because 'duration' column does not exist
CValueError because dates are in wrong format
DNo error, outputs timedelta values
Attempts:
2 left
💡 Hint
Check the data types of the 'start' and 'end' columns before subtraction.
visualization
advanced
2:30remaining
Which plot shows correct monthly sales trend?
Given monthly sales data with datetime index, which plot code correctly shows the sales trend over time?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

sales = pd.Series([100, 150, 200, 250], index=pd.to_datetime(['2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01']))
A
sales.plot()
plt.show()
B
plt.plot(sales.index, sales.values)
plt.show()
C
plt.bar(sales.index, sales.values)
plt.show()
D
plt.scatter(sales.values, sales.index)
plt.show()
Attempts:
2 left
💡 Hint
Think about which plot type best shows a trend over time.
🧠 Conceptual
expert
2:00remaining
Why is timezone handling important in datetime data?
Which statement best explains why handling timezones correctly is crucial in datetime data analysis?
ABecause timezone info is required to convert datetime to string
BBecause timezones determine the date format used in the data
CBecause ignoring timezones can cause incorrect time comparisons and data misalignment
DBecause timezones affect the speed of datetime calculations
Attempts:
2 left
💡 Hint
Think about what happens if you compare times from different zones without adjustment.