0

How do I take a date time column (e.g. format: 2005-06-08 15:30:50) in a pandas dataframe and select / tag only specific times (e.g. 17:45:00) over the entire data set?

I've searched multiple websites for this answer and have not found any addressing the situation of selecting and tagging a specific time stamp across an entire dataframe.

vethno
  • 131
  • 1
  • 2
  • 10
  • 1
    `df['date_col'].dt.time ==pd.to_datetime('15:30:50').time()`? – Quang Hoang Oct 29 '19 at 17:56
  • 1
    Does this answer your question? [Select DataFrame rows between two dates](https://stackoverflow.com/questions/29370057/select-dataframe-rows-between-two-dates) or [How to select column for a specific time range form pandas dataframe in python3?](https://stackoverflow.com/questions/49171911/how-to-select-column-for-a-specific-time-range-from-pandas-dataframe-in-python3) – Trenton McKinney Oct 29 '19 at 17:56

1 Answers1

1

Here is an example:

print(df)

   amount           local_date
0     8.1  2016-09-30-17:45:00
1     4.0  2016-10-02-18:30:00
2     3.0  2016-10-03-17:45:00
3     9.7  2016-10-03-12:20:00
4    10.0  2016-10-04-01:20:32

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

df_filtered=df[df['local_date'].dt.time.apply(lambda x: x.strftime("%H:%M:%S")).eq('17:45:00')]
print(df_filtered)

   amount          local_date
0     8.1 2016-09-30 17:45:00
2     3.0 2016-10-03 17:45:00
ansev
  • 30,322
  • 5
  • 17
  • 31