IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Manipulating Time Series Data in Python

Visualizza il corso

Istruzioni dell'esercizio

We have already imported pandas as pd for you. We have also defined start and end dates.

  • Create monthly_dates using pd.date_range with start, end and frequency alias 'M'.
  • Create and print the pd.Series monthly, passing the list [1, 2] as the data argument, and using monthly_dates as index.
  • Create weekly_dates using pd.date_range with start, end and frequency alias 'W'.
  • Apply .reindex() to monthly three times: first without additional options, then with bfill and then with ffill, print()-ing each result.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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(____)
Modifica ed esegui il codice