I am trying to determine the best way to add a column to my pandas dataframe that converts total minutes (which is in int format) into HH:MM format. I am aware that the easiest way to do this is with datetime, but I can't seem to figure out the best way to convert the data from a Series to make this happen.
Here is the error message:
TypeError: unsupported type for timedelta minutes component: Series
Code (eg 346.0):
df['minutes_asleep_hm'] = str(timedelta(minutes=df['minutes_asleep']))[:-3]
Type:
Name: minutes_asleep, dtype: float64
Series is essentially a list of values. You need to use a map function which applies a given operation to every element in the series and returns the result of this function as a series.
df['minutes_asleep_hm'] = df['minutes_asleep'].map(lambda x: str(timedelta(minutes=x))[:-3], na_action='ignore')
Notes:
x
, though undescriptive, is pretty standard for lambdas and is used here to keep the line short. It represents the current series value that the function is operating on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.