- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Convert the TimeSeries using the series.asfreq() method?
The pandas.Series.asfreq() method is used to convert the Time Series to the specified frequency. By using the parameters of this method we can fill missing(null) values also.
It will return a series object with reindexed frequency, which is specified through the asfreq() method. The parameters of the asfreq() method are freq, method=None, how=None, normalize=False, and fill_value=None. Other than freq remaining all parameters have default values.
Let's create a timeseries object by using the pandas date_range module and apply the asfreq() method.
Example 1
import pandas as pd # create the index index = pd.date_range('2021-07-01', periods=10, freq='H') #creating pandas Series with date index series = pd.Series([1,3,None,5,7,9,None,2,3,4], index=index) print(series) # converted time series print("Converted time series",series.asfreq(freq='2H'))
Explanation
Initially, we imported the pandas module into our workspace by using the python import keyword, then we created an index variable with time sequence data by using the date range function, after that, we have created a pandas series object with a time sequence.
Output
2021-07-01 00:00:00 1.0 2021-07-01 01:00:00 3.0 2021-07-01 02:00:00 NaN 2021-07-01 03:00:00 5.0 2021-07-01 04:00:00 7.0 2021-07-01 05:00:00 9.0 2021-07-01 06:00:00 NaN 2021-07-01 07:00:00 2.0 2021-07-01 08:00:00 3.0 2021-07-01 09:00:00 4.0 Freq: H, dtype: float64 Converted time series 2021-07-01 00:00:00 1.0 2021-07-01 02:00:00 NaN 2021-07-01 04:00:00 7.0 2021-07-01 06:00:00 NaN 2021-07-01 08:00:00 3.0 Freq: 2H, dtype: float64
The initial time series is created by 10 periods with the frequency of hours. Which is displayed in the 1st part of the above output block.
And the converted time series object is also displayed in the above output block, where the frequency is converted to 2hours. And the resultant series is having only 5 elements in it.
Example 2
import pandas as pd # create the index index = pd.date_range('2021-06-30', periods=10, freq='M') #creating pandas Series with date index series = pd.Series(index.month_name(), index=index) print(series) # converted time series print("Converted time series",series.asfreq(freq='Q'))
Explanation
In this above example, we have created a pandas Series using the Date_range method with 10 periods of months frequency. After that, we have changed the frequency of the series object to quarterly.
Output
2021-06-30 June 2021-07-31 July 2021-08-31 August 2021-09-30 September 2021-10-31 October 2021-11-30 November 2021-12-31 December 2022-01-31 January 2022-02-28 February 2022-03-31 March Freq: M, dtype: object Converted time series 2021-06-30 June 2021-09-30 September 2021-12-31 December 2022-03-31 March Freq: Q-DEC, dtype: object
In the above output block, we can see both the initial timeseries object and converted timeseries object. The converted time series is updated to quarterly frequency, by providing the “Q” value to the “freq” parameter of the asfreq method.
- Related Articles
- How to convert the hashtable to the JSON format using PowerShell?
- How to convert a double value into a Java String using format method?
- How to convert a double value into a Java String using append method?
- How to find the covariance using the Series.cov() Method in Pandas?
- How to convert a string into the lower case using JavaScript?
- How to convert the image into a base64 string using JavaScript?
- How to remove elements using the splice() method in JavaScript?
- How to reduce the color using Iterator Method in OpenCV?
- How to get the rows by using pandas series.first() method?
- How to convert a date to a string using the universal time convention?
- How to access and convert time using the time library in Python
- How to get service information with the WMI method using PowerShell?
- How to convert string to JSON using Python?
- How to Convert Kilometres to Miles using Python?
- How to Convert Celsius To Fahrenheit using Python?
