AttributeError: 'TimedeltaProperties' object has no attribute 'minute'
11,665
Solution 1
your column 'time' is of dtype timedelta
as the error tells you; you could use the total_seconds()
method to convert to seconds and divide by 60 to get the minutes.
If you want a full-featured datetime column, combine 'date' and 'time'. Then you can use .dt.minute
.
Ex:
import pandas as pd
df = pd.DataFrame({'time': pd.to_timedelta(['00:30:45','00:30:45','00:21:06','00:21:06','00:21:06']),
'date': pd.to_datetime(['2020-02-28','2020-02-28','2020-03-09','2020-03-09','2020-03-09'])})
# to get the "total minutes":
df['minutes'] = df['time'].dt.total_seconds()/60
df['minutes']
# 0 30.75
# 1 30.75
# 2 21.10
# 3 21.10
# 4 21.10
# Name: minutes, dtype: float64
# to get a column of dtype datetime:
df['DateTime'] = df['date'] + df['time']
# now you can do:
df['DateTime'].dt.minute
# 0 30
# 1 30
# 2 21
# 3 21
# 4 21
# Name: DateTime, dtype: int64
Solution 2
If you have not converted to a datetime dataframe do that first then you create a new column like this
df['minute'] = df['date'].dt.minute
or this method here
df[new]= df[column].map(lambda x: datetime.datetime(x.minutes))
Related videos on Youtube
Author by
Le Noff
Updated on June 04, 2022Comments
-
Le Noff almost 2 years
I have a dataframe that looks like this
df [output]: date time 2020-02-28 00:30:45 2020-02-28 00:30:45 2020-03-09 00:21:06 2020-03-09 00:21:06 2020-03-09 00:21:06
with
df.time.dtype [output]: dtype('<m8[ns]')
I want to extract the minutes in the time variable with the following command
df.time.dt.minute
but instead, I have this error
AttributeError: 'TimedeltaProperties' object has no attribute 'minute'
Does someone know how to fix this problem?
-
Le Noff almost 4 yearsThanks for your answer. I just tried and now it gives me the "AttributeError: 'Timedelta' object has no attribute 'minutes'" error. I don't understand the difference between 'Timedelta' and 'TimedeltaProperties'.
-
FObersteiner almost 4 yearsthis doesn't make much sense;
df['date'].dt.minute
would only return zeros...