Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'TimedeltaProperties' object has no attribute 'minute'

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?

like image 888
Le Noff Avatar asked Jun 09 '20 11:06

Le Noff


2 Answers

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

[pd.Timedelta docs]

# 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
like image 164
FObersteiner Avatar answered Sep 28 '22 08:09

FObersteiner


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))
like image 36
Osuolale Emmanuel Avatar answered Sep 28 '22 06:09

Osuolale Emmanuel