(Mar-17-2022, 01:32 AM)deanhystad Wrote: Is it better to make a column of strings and then convert to datetime? I assumed that is what happens here:I could have done it one step,which would have saved me a line.
Looking at it again so is your post #8 fine,just grab a datetime object and use it filtering is good👍
I was more thinking about post #2 where you did use datetime from standard library.
So update make code to use one step and as a example doing it the other way around dropping DateTime if want the original split in Date and Time.
import pandas as pd from io import StringIO data = """\ 3/8/22, 4:06 PM, String Value 3/8/22, 4:12 PM, String Value 3/8/22, 4:12 PM, String Value 3/8/22, 4:13 PM, String Value 3/8/22, 4:14 PM, String Value 3/8/22, 4:14 PM, String Value 3/8/22, 4:15 PM, String Value 3/8/22, 4:15 PM, String Value 3/8/22, 4:15 PM, String Value 3/8/22, 4:15 PM, String Value 3/8/22, 4:17 PM, String Value 3/8/22, 4:17 PM, String Value""" df = pd.read_csv(StringIO(data), sep=",", names=["Date", "Time", "Comment"]) df['DateTime'] = pd.to_datetime(df["Date"] + df["Time"]) mask = (df['DateTime'] > '2022-03-08 16:06:00') & (df['DateTime'] >= '2022-03-08 16:15:00') df_new = df.loc[mask] df_new = df_new.drop(columns=['DateTime']) print(df_new)
Output: Date Time Comment
6 3/8/22 4:15 PM String Value
7 3/8/22 4:15 PM String Value
8 3/8/22 4:15 PM String Value
9 3/8/22 4:15 PM String Value
10 3/8/22 4:17 PM String Value
11 3/8/22 4:17 PM String Value