Working with DateTime columns
pandas
loads datetime columns as an object
data type by default. Having text dates aren't much use for time series analysis, so you must be able to convert those columns into a datetime
datatype. This data type allows you to work more flexibly with pandas
dates and extract useful features from them.
Another stocks dataset, this time for Apple, has been loaded as apple
.
This exercise is part of the course
Anomaly Detection in Python
Exercise instructions
- Convert the
Date
column ofapple
to adatetime
column. - Extract the day of the week and store it in a new column called
day_of_week
. - Extract the month number and store it in a new column called
month
. - Extract the day of the month and store it in a new column called
day_of_month
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Convert the Date column to DateTime
apple['Date'] = ____
# Create a column for the day of the week
apple['day_of_week'] = ____
# Create a column for the month
apple['month'] = ____
# Create a column for the day of the month
apple['day_of_month'] = ____
print(apple[['day_of_week', 'month', 'day_of_month']])