CommencerCommencer gratuitement

Subset time series data

When plotting time series data, you may occasionally want to visualize only a subset of the data. The pandas library provides powerful indexing and subsetting methods that allow you to extract specific portions of a DataFrame. For example, you can subset all the data between 1950 and 1960 in the discoveries DataFrame by specifying the following date range:

subset_data = discoveries['1950-01-01':'1960-01-01']

Note: Subsetting your data this way is only possible if the index of your DataFrame contains dates of the datetime type. Failing that, the pandas library will return an error message.

Cet exercice fait partie du cours

Visualizing Time Series Data in Python

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Select the subset of data between 1945 and 1950
discoveries_subset_1 = discoveries['____':'____']

# Plot the time series in your DataFrame as a blue area chart
ax = discoveries_subset_1.____(color='blue', fontsize=15)

# Show plot
plt.show()
Modifier et exécuter le code