AttributeError: 'TimedeltaProperties' 对象没有属性 'minute' [英] AttributeError: 'TimedeltaProperties' object has no attribute 'minute'

查看:66
本文介绍了AttributeError: 'TimedeltaProperties' 对象没有属性 '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

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

但是,我有这个错误

AttributeError: 'TimedeltaProperties' 对象没有属性 'minute'

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

有人知道如何解决这个问题吗?

Does someone know how to fix this problem?

推荐答案

你的列 'time' 是 dtype timedelta 正如错误告诉你的那样;您可以使用 total_seconds() 方法转换为秒并除以 60 以获得分钟.

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.

如果您想要功能齐全的日期时间列,请结合使用日期"和时间".然后你可以使用.dt.minute.

If you want a full-featured datetime column, combine 'date' and 'time'. Then you can use .dt.minute.

例如:

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 文档]

# 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

这篇关于AttributeError: 'TimedeltaProperties' 对象没有属性 'minute'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆