In this tutorial, we will learn how to get rows from a Pandas dataframe by using their Datetime index.
How to get a row in Pandas using its Datetime Index?
To get Pandas dataframe’s rows using a datetime index, the syntax will be-
row = df.loc[date]
Here,
df
— The Pandas DataFrame object.df.loc[]
— A property of Pandas DataFrame to get rows using their row labels.
In the above code, we have a dataframe df
with datetime indices. We pass a date to df.loc[]
to fetch the required row using its respective date index.
Note that loc[]
is Pandas DataFrame’s property used to fetch rows by their respective row labels.
Examples
To understand the above code, we will see some examples. We will use weather data for the first week of October in a city for our examples.
import pandas as pd d = { "Max. Temp." : [41.7, 36.5, 37.7, 35.9, 38.7, 40.2, 40.5], "Min. Temp." : [28.8, 25.5, 26.6, 26.3, 24.9, 28.8, 29.6] } index = pd.date_range("2022-10-01", "2022-10-07") df = pd.DataFrame(d, index=index) print(df)
Output:
Max. Temp. Min. Temp. 2022-10-01 41.7 28.8 2022-10-02 36.5 25.5 2022-10-03 37.7 26.6 2022-10-04 35.9 26.3 2022-10-05 38.7 24.9 2022-10-06 40.2 28.8 2022-10-07 40.5 29.6
Here you can see that the dataframe has dates as its index. This is implemented by passing the range of dates we have created using the pd.date_range()
function.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
The pd.date_range()
function takes two dates as input and returns the range of dates between them. Also, the data format used by Pandas is yyyy-mm-dd. It also accepts mm-dd-yyyy formats, but we shall stick with the former.
Example 1: Get the row at a specific datetime index
To get the row at a specific datetime index, we pass the datetime index of the required row to df.loc[]
.
row = df.loc['2022-10-03'] print(row)
Output:
Max. Temp. 37.7 Min. Temp. 26.6 Name: 2022-10-03 00:00:00, dtype: float64
df.loc[]
returns a row as a Pandas Series object. But, the column names of the dataframe become the row labels, while the row label of the row in dataframe is set as the name of the Series object.
Example 2: Get rows in a range of continuous datetime indices
To get rows in a range of datetime indices, we use the loc[]
property of Pandas to slice out the range. Let’s try to get the rows with dates from the 2nd to the 5th of October.
continuous_range_of_date_rows = df.loc['2022-10-02':'2022-10-05'] print(continuous_range_of_date_rows)
Output:
Max. Temp. Min. Temp. 2022-10-02 36.5 25.5 2022-10-03 37.7 26.6 2022-10-04 35.9 26.3 2022-10-05 38.7 24.9
As we see in the output, we have the rows in the given range of dates. Here, df.loc[]
returns a Pandas DataFrame object with the same column names as the original dataframe.
Note that the df.loc[]
returns a Series object when it is returning one row, but it returns a DataFrame when returning multiple rows.
Example 3: Get rows in a series of a non-continuous date range.
To get rows in a range of non-continuous datetime indices, we use the loc
property of the Pandas dataframe to slice out the range. Let’s try to get the rows with the dates 2nd, 3rd, and 6th of October. For that, we will pass the required list of dates to df.loc[]
.
non_continuous_range_of_date_rows = df.loc[['2022-10-02', '2022-10-03', '2022-10-06']] print(non_continuous_range_of_date_rows)
Output:
Max. Temp. Min. Temp. 2022-10-02 36.5 25.5 2022-10-03 37.7 26.6 2022-10-06 40.2 28.8
Thus, we have a dataframe as our output for the list of dates we have passed to df.loc[]
.
Summary
From this tutorial, we learned how to:
- Create a Pandas DataFrame with a datetime index.
- Use
loc
to obtain a row with a specific datetime index. - Get rows in a continuous range of datetime indices using
loc
. - Obtain rows in a non-continuous range of datetime indices using
loc
.
You might also be interested in –
- Pandas – How to Create a date range?
- Pandas – Get only date from datetime
- Pandas – Get Day of Week from Date
- Pandas – Change Format of Date Column
- Pandas – Extract Year from a datetime column
- Get Quarter from Date in Pandas
- Remove Time from Date in Pandas
- Pandas – Category Column with Datetime Values
- Pandas – Get Month from Date
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.