Accessing Specific DateTime Element

The below example demonstrates how we access specific DateTime element of DateTime object.

Python3




import pandas as pd
from datetime import datetime
import numpy as np
  
range_data = pd.date_range(start ='1/1/2019', end ='1/08/2019', freq ='Min')
df = pd.DataFrame(range_data, columns =['date'])
df['data'] = np.random.randint(0, 100, size =(len(range_data)))
  
df['datetime'] = pd.to_datetime(df['date'])
df = df.set_index('datetime')
df.drop(['date'], axis = 1, inplace = True)
  
print(df['2019-01-05'][1:11])


Output

                     data
datetime                 
2019-01-05 00:01:00    99
2019-01-05 00:02:00    21
2019-01-05 00:03:00    29
2019-01-05 00:04:00    98
2019-01-05 00:05:00     0
2019-01-05 00:06:00    72
2019-01-05 00:07:00    69
2019-01-05 00:08:00    53
2019-01-05 00:09:00     3
2019-01-05 00:10:00    37

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.

Similar Reads

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....

Determine the Data Type of an Element in the DateTime Range

...

Create DataFrame with DateTime Index

To determine the type of an element in the DateTime range, we use indexing to fetch the element and then use the type function to know its data type....

Convert DateTime elements to String format

...

Accessing Specific DateTime Element

To create a DataFrame with a DateTime index, we first need to create a DateTime range and then pass it to pandas.DataFrame method....

Conclusion

...