vethno
vethno

Reputation: 141

Select Specific Times from Multiple Days in Pandas Timeseries Dataframe

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.

Upvotes: 0

Views: 745

Answers (1)

ansev
ansev

Reputation: 30920

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

Upvotes: 1

Related Questions