How to utilize time series in Pandas?
The pandas library in python provides a standard set of time series tools and data algorithms. By this, we can efficiently work with very large time series and easily slice and dice, aggregate, and resample irregular and fixed frequency time series.
Time series data is an important form of structured data which is used in finance, economics, ecology, etc. Anything that is observed or measured at many points in time forms a time series.
- Timestamps: These are the specific instants in time
- Fixed period: This will represent such as the month of May 25 or the full year 1999.
Modules in DateTime
- date: This module is used to store the calendar in the format of year, month, date.
- time: This module is used to get and display the time in the format of hours, minutes, seconds, and microseconds.
- datetime: This module is used to store both date and time.
- timedelta: This module is used to get the difference between two datetime values.
Below are various examples that depict how to utilize time series in the pandas library:
Example 1: Display the current date and time. In this program, we are going to display current date and time.
Python3
from datetime import datetime
datetime.now()
|
Output:
Example 2: Program to display hour, minutes, seconds, month, year, day individually from the module.
Python3
from datetime import datetime
a = datetime.now()
print (a.year)
print (a.day)
print (a.month)
print (a.hour)
print (a.minute)
print (a.second)
print (a.microsecond)
print (a.date)
|
Output:
Example 3: Difference between two dates. We can get hours, days and minute difference using timedelta module.
Python3
from datetime import timedelta
deltaresult = datetime( 2027 , 5 , 7 ) - datetime( 2021 , 6 , 24 )
print (deltaresult)
print (deltaresult.days)
print (deltaresult.seconds)
|
Output:
If we want to generate time series data python will support date_range module. This will generate dates within the given frequency. It is available in the pandas module.
Syntax: pandas.date_range(start=None, end=None, periods=None, freq=None)
Parameters:
- start: start date time from start date.
- end: specify end date time.
- freq: represents a frequency like hours or minutes or seconds.
Example 4: In this program, we can start with 2021 January 1st and display the dates up to march using the date_range method.
Python3
import pandas as pd
dates = pd.date_range( '1/1/2021' , periods = 75 )
print (dates)
|
Output:
Generating values to corresponding dates by using timeseries as indices.
Example 5: In this program, we are giving values to dates by setting dates as indexes to each value.
Python3
import pandas as pd
import numpy as np
dates = pd.date_range( '1/1/2021' , periods = 75 )
results = pd.Series(np.arange( 75 ), index = dates)
print (results)
print (pd.DataFrame(results))
|
Output:
We can convert data string columns to datetime type by using the below method.
Syntax:
pandas.to_datetime(column_name)
Example 6: In this program, we will convert string data into datetime type.
Python3
import pandas as pd
data = pd.DataFrame({ 'dates' : [ '1/2/2021' ,
'2/4/2020' ,
'1/3/2021' ,
'4/12/2017' ],
'customers' : [ 100 , 30 , 56 , 56 ]})
data
data[ 'dates' ] = pd.to_datetime(data[ 'dates' ])
data
|
Output:
Example 7: In this program, we are going to take some time series data as an index, convert it and verify whether they are equal or not.
Python3
import pandas as pd
data = pd.DataFrame({ 'dates' : [ '1/2/2021' , '2/4/2020' ,
'1/3/2021' , '4/12/2017' ,
'1/2/2021' , '2/4/2020' ,
'1/3/2021' ],
'customers' : [ 100 , 30 , 56 ,
56 , 23 , 45 , 67 ]})
data
data[ 'dates' ] = pd.to_datetime(data[ 'dates' ])
data
print (data[ 'dates' ].nunique())
data[ 'dates' ].value_counts()
|
Output:
Example 8: Program to display a histogram from a data frame having DateTime objects.
Python3
import pandas as pd
data = pd.DataFrame({ 'dates' : [ '1/2/2021' , '2/4/2020' ,
'1/3/2021' , '4/12/2017' ,
'1/2/2021' , '2/4/2020' ,
'1/3/2021' ],
'customers' : [ 100 , 30 , 56 ,
56 , 23 , 45 , 67 ]})
data
data[ 'dates' ] = pd.to_datetime(data[ 'dates' ])
data[ 'dates' ].hist(figsize = ( 10 , 5 ), color = "green" )
|
Output:
Last Updated :
03 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...