pandas: selecting rows in a specific time window

2024/10/8 0:24:04

I have a dataset of samples covering multiple days, all with a timestamp. I want to select rows within a specific time window. E.g. all rows that were generated between 1pm and 3 pm every day.

This is a sample of my data in a pandas dataframe:

22           22  2018-04-12T20:14:23Z  2018-04-12T21:14:23Z      0  6370.1   
23           23  2018-04-12T21:14:23Z  2018-04-12T21:14:23Z      0  6368.8   
24           24  2018-04-12T22:14:22Z  2018-04-13T01:14:23Z      0  6367.4   
25           25  2018-04-12T23:14:22Z  2018-04-13T01:14:23Z      0  6365.8   
26           26  2018-04-13T00:14:22Z  2018-04-13T01:14:23Z      0  6364.4   
27           27  2018-04-13T01:14:22Z  2018-04-13T01:14:23Z      0  6362.7   
28           28  2018-04-13T02:14:22Z  2018-04-13T05:14:22Z      0  6361.0   
29           29  2018-04-13T03:14:22Z  2018-04-13T05:14:22Z      0  6359.3   
..          ...                   ...                   ...    ...     ...   
562         562  2018-05-05T08:13:21Z  2018-05-05T09:13:21Z      0  6300.9   
563         563  2018-05-05T09:13:21Z  2018-05-05T09:13:21Z      0  6300.7   
564         564  2018-05-05T10:13:14Z  2018-05-05T13:13:14Z      0  6300.2   
565         565  2018-05-05T11:13:14Z  2018-05-05T13:13:14Z      0  6299.9   
566         566  2018-05-05T12:13:14Z  2018-05-05T13:13:14Z      0  6299.6   

How do I achieve that? I need to ignore the date and just evaluate the time component. I could traverse the dataframe in a loop and evaluate the date time in that way, but there must be a more simple way to do that..

I converted the messageDate which was read a a string to a dateTime by

df["messageDate"]=pd.to_datetime(df["messageDate"])

But after that I got stuck on how to filter on time only.

Any input appreciated.

Answer

datetime columns have DatetimeProperties object, from which you can extract datetime.time and filter on it:

import datetimedf = pd.DataFrame(['2018-04-12T12:00:00Z', '2018-04-12T14:00:00Z','2018-04-12T20:00:00Z','2018-04-13T12:00:00Z', '2018-04-13T14:00:00Z', '2018-04-13T20:00:00Z'], columns=['messageDate']
)
dfmessageDate
# 0 2018-04-12 12:00:00
# 1 2018-04-12 14:00:00
# 2 2018-04-12 20:00:00
# 3 2018-04-13 12:00:00
# 4 2018-04-13 14:00:00
# 5 2018-04-13 20:00:00df["messageDate"] = pd.to_datetime(df["messageDate"])
time_mask = (df['messageDate'].dt.hour >= 13) & \(df['messageDate'].dt.hour <= 15)df[time_mask]
#           messageDate
# 1 2018-04-12 14:00:00
# 4 2018-04-13 14:00:00
https://en.xdnf.cn/q/70177.html

Related Q&A

How to visualize (dendrogram) a dictionary of hierarchical items?

This is my first time of doing visualization from hierarchical data in dictionary format with Python. Last part of the data looks like this:d = {^2820: [^391, ^1024], ^2821: [^759, w, ^118, ^51], ^2822…

How to unfocus (blur) Python-gi GTK+3 window on Linux

What I want to do and whyI want my window to unfocus, so the previous focused window is selected.Why? I want to interact with the previously selected window (from other programs). My current plan is: …

SyntaxError: multiple exception types must be parenthesized

I am a beginner and have a problem after installing pycaw for the audio control using python, on putting the basic initialization code for pycaw, i get the following error:- Traceback (most recent call…

Python find difference between file paths

I have a bunch of file paths, such as:path1 = "./base/folder1/subfolder" path2 = "./base/folder2/"I am trying to write a function that can give me the relative difference between th…

Update range of colorbar in matplotlib

I want to update a contourf plot within a function, which works fine. However, the range of the data changes and I therefore also have to update the colorbar. That is where I fail to do so.Please see f…

Anaconda - arrow keys not work well in python shell

I installed Anaconda3 on manjaro (with i3wm and Urxvt). When I go into python interpreter, it is OK to type python script and execute. But when key arrows are pressed to call up history, everything mes…

couldnt remove origin point in matplotlib polycollection

I have tried an example with PolyCollection from matplotlib tutorials and noticed one strange thing. I couldnt remove this points from axes origin see fig. How do I manage this?from mpl_toolkits.mplot…

How to read .odt using Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.The com…

Modifying Django settings in tests

From Django docs:You shouldn’t alter settings in your applications at runtime. Forexample, don’t do this in a view:from django.conf import settingssettings.DEBUG = True # Dont do this!The only plac…

How To Use Django Cycle Tag

This hopefully is a pretty easy question. My endgoal is to be able to view my database entries in a table which I can sort via the column headers. I have read the documentation on cycle tags, but dont …