- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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'))
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.
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.
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'))
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.
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.
- How do we upsample a time-series using asfreq() method?
- Python Pandas - Convert the DateTimeIndex to Series
- How to get items from a series object using the get() method?
- How to handle the null values while comparing the two series objects using series.eq() method?
- How to remove a specified row From the Pandas Series Using Drop() method?
- Python Pandas - Convert the DateTimeIndex to Series excluding the TimeZone
- How does the pandas Series idxmax() method work?
- How does the pandas Series idxmin() method work?
- How to replace NaN values of a series with the mean of elements using the fillna() method?
- How to retrieve the last valid index from a series object using pandas series.last_valid_index() method?
- How to get the final rows of a time series data using pandas series.last() method?
- How to convert the hashtable to the JSON format using PowerShell?
- How to use the series.isin() method to check values in a series?
- How to access pandas Series elements using the .iloc attribute?
- How to access pandas Series elements using the .loc attribute?