Convert monthly to weekly data
You have learned in the video how to use .reindex()
to conform an existing time series to a DateTimeIndex
at a different frequency.
Let's practice this method by creating monthly data and then converting this data to weekly frequency while applying various fill logic options.
This is a part of the course
“Manipulating Time Series Data in Python”
Exercise instructions
We have already imported pandas
as pd
for you. We have also defined start
and end
dates.
- Create
monthly_dates
usingpd.date_range
withstart
,end
and frequency alias'M'
. - Create and print the pd.Series
monthly
, passing the list[1, 2]
as thedata
argument, and usingmonthly_dates
asindex
. - Create
weekly_dates
usingpd.date_range
withstart
,end
and frequency alias'W'
. - Apply
.reindex()
tomonthly
three times: first without additional options, then withbfill
and then withffill
,print()
-ing each result.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set start and end dates
start = '2016-1-1'
end = '2016-2-29'
# Create monthly_dates here
monthly_dates = ____
# Create and print monthly here
monthly = ____
print(____)
# Create weekly_dates here
weekly_dates = ____
# Print monthly, reindexed using weekly_dates
print(____)
print(____)
print(____)
This exercise is part of the course
Manipulating Time Series Data in Python
In this course you'll learn the basics of working with time series data.
This chapter dives deeper into the essential time series functionality made available through the pandas DataTimeIndex. It introduces resampling and how to compare different time series by normalizing their start points.
Exercise 1: Compare time series growth ratesExercise 2: Compare the performance of several asset classesExercise 3: Comparing stock prices with a benchmarkExercise 4: Plot performance difference vs benchmark indexExercise 5: Changing the time series frequency: resamplingExercise 6: Convert monthly to weekly dataExercise 7: Create weekly from monthly unemployment dataExercise 8: Upsampling & interpolation with .resample()Exercise 9: Use interpolation to create weekly employment dataExercise 10: Interpolate debt/GDP and compare to unemploymentExercise 11: Downsampling & aggregationExercise 12: Compare weekly, monthly and annual ozone trends for NYC & LAExercise 13: Compare monthly average stock prices for Facebook and GoogleExercise 14: Compare quarterly GDP growth rate and stock returnsExercise 15: Visualize monthly mean, median and standard deviation of S&P500 returnsWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.