Create DateTime Values with Pandas
To create a DateTime series using Pandas, we need the DateTime module and then we can create a DateTime range with the date_range method.
Example
Python3
import pandas as pd from datetime import datetime import numpy as np range_date = pd.date_range(start = '1/1/2019' , end = '1/08/2019' , freq = 'Min' ) print (range_date) |
DatetimeIndex(['2019-01-01 00:00:00', '2019-01-01 00:01:00', '2019-01-01 00:02:00', '2019-01-01 00:03:00', '2019-01-01 00:04:00', '2019-01-01 00:05:00', '2019-01-01 00:06:00', '2019-01-01 00:07:00', '2019-01-01 00:08:00', '2019-01-01 00:09:00', ... '2019-01-07 23:51:00', '2019-01-07 23:52:00', '2019-01-07 23:53:00', '2019-01-07 23:54:00', '2019-01-07 23:55:00', '2019-01-07 23:56:00', '2019-01-07 23:57:00', '2019-01-07 23:58:00', '2019-01-07 23:59:00', '2019-01-08 00:00:00'], dtype='datetime64[ns]', length=10081, freq='T')
Explanation:
Here in this code, we have created the timestamp based on minutes for date ranges from 1/1/2019 to 8/1/2019.
We can vary the frequency by hours to minutes or seconds.
This function will help you to track the record of data stored per minute. As we can see in the output the length of the datetime stamp is 10081.
Note: Remember pandas use data type as datetime64[ns].
Basic of Time Series Manipulation Using Pandas
Although the time series is also available in the Scikit-learn library, data science professionals use the Pandas library as it has compiled more features to work on the DateTime series. We can include the date and time for every record and can fetch the records of DataFrame.
We can find out the data within a certain range of dates and times by using the DateTime module of Pandas library.
Let’s discuss some major objectives of time series analysis using Pandas library.
Objectives of Time Series Analysis
- Create a series of date
- Work with data timestamp
- Convert string data to timestamp
- Slicing of data using timestamp
- Resample your time series for different time period aggregates/summary statistics
- Working with missing data
Now, let’s do some practical analysis of some data to demonstrate the use of Pandas’ time series.