Convert DateTime elements to String format
The below example demonstrates how we can convert the DateTime elements of DateTime object to string format.
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' ) df = pd.DataFrame(range_date, columns = [ 'date' ]) df[ 'data' ] = np.random.randint( 0 , 100 , size = ( len (range_date))) string_data = [ str (x) for x in range_date] print (string_data[ 1 : 11 ]) |
Output:
['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-01 00:10:00']
Explanation:
This code just uses the elements of data_rng and converts them to string and due to a lot of data we slice the data and print the first ten values list string_data.
By using the for each loop in the list, we got all the values that are in the series range_date. When we are using date_range we always have to specify the start and end date.
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.