Home > Net >  Python Pandas - How to convert datetime64[ns] and get hours and minutes only from it?
Python Pandas - How to convert datetime64[ns] and get hours and minutes only from it?

Time:04-19

I have a pandas dateframe that contains a datetime64[ns] column named, 'Submit Date.' From this row of syntax, it will output a row value as:

Code:

TicketsDF[['submitDate']] = (TicketsDF[['submitDate']].apply(pd.to_datetime, unit="s")) - timedelta(hours= 5)

Output is:

2022-04-15 15:52.37

How do I just grab the hours and minutes from this value? I'm needing to grab it because later on, I need to check to see if that time is between a start and end time.

CodePudding user response:

You can use the dt.time for hours and minutes.

out = TicketsDF[['submitDate']].dt.time

Here is more information:

https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.time.html

CodePudding user response:

You can use pandas.Series.dt.strftime which returns string type

TicketsDF['submitDate'].dt.strftime('%H:%M')

If you care about the type, you can try remove seconds and use dt.time accessor.

TicketsDF['submitDate'].dt.floor('min').dt.time
  • Related