I have a dataframe df
with column date
in format 'YYYY-mm-dd HH:MM:SS.miliseconds' (datetime64[ns]). I want to generate another column, minutes
(float), as the number of minutes counted from time 00:00:00 to time HH:MM:SS. How can I do that?
Asked
Active
Viewed 203 times
0

FObersteiner
- 22,500
- 8
- 42
- 72

NigelBlainey
- 49
- 9
-
1Simple arithmetic: `minutes = HH*60 + MM + SS/60` – Barmar Mar 28 '23 at 20:43
-
Possible duplicate: https://stackoverflow.com/questions/50392796/how-to-convert-a-datetime-format-to-minutes-pandas – MattDMo Mar 28 '23 at 20:49
-
But how can I make python identify HH, MM, and SS? – NigelBlainey Mar 28 '23 at 20:49
-
1`datetime` has accessors for `hour`, `minute`, `second`. It will be along the lines of `df["minutes"] = df["date"].dt.hour * 60 + ...` – motto Mar 28 '23 at 21:25
-
Number of minutes since when? – ctrl-alt-delor Mar 29 '23 at 09:22
2 Answers
2
you can easily convert to any unit of time since midnight by
- subtracting the date from the datetime to get a timedelta (duration),
- then convert that timedelta to the unit you require;
EX:
import pandas as pd
df = pd.DataFrame({"datetime": ["2016-01-01 00:00:00",
"2016-03-01 12:00:00",
"2016-06-01 23:59:00"]})
df["datetime"] = pd.to_datetime(df["datetime"])
# subtract the datetime floored to the day to get a timedelta,
df["minutes_from_midnight"] = (
(df["datetime"] - df["datetime"].dt.floor("d"))
.dt.total_seconds() # then convert to minutes
.div(60)
)
df
datetime minutes_from_midnight
0 2016-01-01 00:00:00 0.0
1 2016-03-01 12:00:00 720.0
2 2016-06-01 23:59:00 1439.0

FObersteiner
- 22,500
- 8
- 42
- 72
0
I used the following solution. First, I filtered the hours, minutes, and seconds from the timestamp (column date
):
df['time_hours'] = df.date.dt.hour
df['time_minutes'] = df.date.dt.minute
df['time_seconds'] = df.date.dt.second
Then, I defined a function to calculate total time in minutes, using simple algebra:
def get_total_minutes(row):
time_hours = row['time_hours']
time_minutes = row['time_minutes']
time_seconds = row['time_seconds']
total_minutes = float((time_hours*60)+(time_minutes)+(time_seconds/60))
return total_minutes
Finally, I applied the function to each row and got the intended results:
df['minutes'] = df.apply(lambda row: get_total_minutes(row), axis=1)

NigelBlainey
- 49
- 9