How to convert 'YYYY-mm-dd HH:MM:SS.[]' to number of minutes in the date?
Question:
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?
Answers:
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
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)
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?
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
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)