
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to resample a given time series data and find the maximum month-end frequency
Assume, you have time series and the result for maximum month-end frequency,
DataFrame is: Id time_series 0 1 2020-01-05 1 2 2020-01-12 2 3 2020-01-19 3 4 2020-01-26 4 5 2020-02-02 5 6 2020-02-09 6 7 2020-02-16 7 8 2020-02-23 8 9 2020-03-01 9 10 2020-03-08 Maximum month end frequency: Id time_series time_series 2020-01-31 4 2020-01-26 2020-02-29 8 2020-02-23 2020-03-31 10 2020-03-08
Solution
To solve this, we will follow the steps given below −
Define a dataframe with one column,
d = {'Id': [1,2,3,4,5,6,7,8,9,10]} df = pd.DataFrame(d)
Create date_range function inside start=’01/01/2020’, periods = 10 and assign freq = ‘W’. It will generate ten dates from given start date to next weekly start dates and store it as df[‘time_series’].
df['time_series'] = pd.date_range('01/01/2020', periods=10, freq='W')
Apply resample method to find the maximum month end frequency,
df.resample('M', on='time_series').max())
Example
Let’s see the below implementation to get a better understanding −
import pandas as pd d = {'Id': [1,2,3,4,5,6,7,8,9,10]} df = pd.DataFrame(d) df['time_series'] = pd.date_range('01/01/2020', periods=10, freq='W') print("DataFrame is:\n",df) print("Maximum month end frequency: ") print(df.resample('M', on='time_series').max())
Output
DataFrame is: Id time_series 0 1 2020-01-05 1 2 2020-01-12 2 3 2020-01-19 3 4 2020-01-26 4 5 2020-02-02 5 6 2020-02-09 6 7 2020-02-16 7 8 2020-02-23 8 9 2020-03-01 9 10 2020-03-08 Maximum month end frequency: Id time_series time_series 2020-01-31 4 2020-01-26 2020-02-29 8 2020-02-23 2020-03-31 10 2020-03-08
- Related Articles
- Write a program in Python to print the first and last three days from a given time series data
- Write a program in Python to find the maximum length of a string in a given Series
- Write a Python program to find the maximum value from first four rows in a given series
- Write a program in Python to find the index for NaN value in a given series
- Python Pandas - Extract month number from the DateTimeIndex with specific time series frequency
- Write a program in Python to find the missing element in a given series and store the full elements in the same series
- Write a Python program to shuffle all the elements in a given series
- Write a program in Python to count the total number of integer, float and object data types in a given series
- Write a program in Python to round all the elements in a given series
- Write a program to truncate a dataframe time series data based on index value
- Write a program in Python to filter valid dates in a given series
- Write a program in Python to filter armstrong numbers in a given series
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Write a program in Python to filter only integer elements in a given series
- Write a program in Python to find the most repeated element in a series

Advertisements