Home » Pandas: How to Select Rows of DataFrame by Timestamp

Pandas: How to Select Rows of DataFrame by Timestamp

by Tutor Aspire

You can use the following basic syntax to select rows between two timestamps in a pandas DataFrame:

df[(df['tstamp'] > '2022-10-25 04:30:00') & (df['tstamp'] 2022-10-27 11:00:00')]

This syntax assumes that tstamp already has a dtype of datetime.

If it doesn’t, you can use the following syntax to convert it to a datetime column:

df['tstamp'] = pd.to_datetime(df['tstamp'])

The following example shows how to use this syntax in practice.

Example: Select Rows of Pandas DataFrame by Timestamp

Suppose we have the following pandas DataFrame that contains information about sales at some retail store:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'tstamp': ['2022-10-25 04:00:00', '2022-10-25 11:55:12',
                                 '2022-10-26 02:00:00', '2022-10-27 10:30:00',
                                 '2022-10-27 14:25:00', '2022-10-28 01:15:27'],
                   'sales': [18, 22, 19, 14, 14, 11]})

#view DataFrame
print(df)

                tstamp  sales
0  2022-10-25 04:00:00     18
1  2022-10-25 11:55:12     22
2  2022-10-26 02:00:00     19
3  2022-10-27 10:30:00     14
4  2022-10-27 14:25:00     14
5  2022-10-28 01:15:27     11

Suppose we would like to select only the rows between the following two timestamps:

  • 2022-10-25 04:30:00
  • 2022-10-27 11:00:00

We can use the following syntax to do so:

#convert timestamp column to datetime dtype
df['tstamp'] = pd.to_datetime(df['tstamp'])

#select rows between two timestamps
df[(df['tstamp'] > '2022-10-25 04:30:00') & (df['tstamp'] 2022-10-27 11:00:00')]

	tstamp	             sales
1	2022-10-25 11:55:12	22
2	2022-10-26 02:00:00	19
3	2022-10-27 10:30:00	14

Notice that only the rows between the two timestamps that we specified are selected.

Also note that you can select rows by timestamp using only a date value.

For example, we could use the following code to select all rows where the timestamp is greater than 2022-10-27:

#convert timestamp column to datetime dtype
df['tstamp'] = pd.to_datetime(df['tstamp'])

#select rows with timestamp after 2022-10-27
df[df['tstamp'] > '2022-10-27']

	tstamp	             sales
3	2022-10-27 10:30:00	14
4	2022-10-27 14:25:00	14
5	2022-10-28 01:15:27	11

Notice that only the rows where the value in the tsamp column are after 2022-10-27 are selected.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Convert Datetime to Date in Pandas
How to Convert Columns to DateTime in Pandas
How to Sort a Pandas DataFrame by Date

You may also like