'list' 对象没有属性 'strptime'

'list' object has no attribute 'strptime'

这里我想读取包含在 csv 文件中的 24 小时格式的时间。我写了 class 以格式 %H:%M:%S 转换时间。但是我得到一个错误

'list' object has no attribute 'strptime'

谁能帮忙解决这个问题?这里我 post 我的代码。

import pandas as pd
import time
import datetime

def convertTime(s):
    tm = time.strptime(s, "%H:%M:%S")
    return datetime.datetime.strptime(tm.tm_hour, tm.tm_min, tm.tm_sec)

data = pd.read_csv('x.csv')
row_num = 0
for row in data:
    if(row_num == 0):
    time.append(convertTime(row[0]))

这是我的 csv 文件的子集

time         g
6:15:00   141
9:00:00   0
9:25:00   95
9:30:00   0
11:00:00  149

您在代码(或命令行)的某处创建了一个全局列表 time(参见 time.append(...))。此列表隐藏了您打算在函数中使用的模块 time。换句话说:time 是一个列表。给那个列表另一个名字。

这是否达到你想要的效果?

df['time'] = pd.to_datetime(df['time'], format='%H:%M:%S').dt.time
df.set_index('time', inplace=True)

# plotting
df.plot()