Calculate time offset in days - part 2
Great work! Now, we have six different data sets with year, month and day values for Invoice and Cohort dates - invoice_year
, cohort_year
, invoice_month
, cohort_month
, invoice_day
, and cohort_day
.
In this exercise you will calculate the difference between the Invoice and Cohort dates in years, months and days separately and then calculate the total days difference between the two. This will be your days offset which we will use in the next exercise to visualize the customer count. The online
data has been loaded, you can print its header to the console by calling online.head()
.
This exercise is part of the course
Customer Segmentation in Python
Exercise instructions
- Find the difference in years between the moment of the invoice and the moment the cohort was created.
- Find the difference in months between the moment of the invoice and the moment the cohort was created.
- Find the difference in days between the moment of the invoice and the moment the cohort was created.
- Calculate the number of days for the
CohortIndex
(assume 365 days in a year, and 30 days in a month).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Calculate difference in years
years_diff = ____ - ____
# Calculate difference in months
months_diff = ____ - ____
# Calculate difference in days
days_diff = ____ - ____
# Extract the difference in days from all previous values
online['CohortIndex'] = years_diff * ____ + months_diff * ____ + days_diff + ____
print(online.head())