-2

Here I want to read time in 24 hours format include in csv file. I wrote the class to convert time in format %H:%M:%S. but I got an error

'list' object has no attribute 'strptime'

Can anyone help to solve this? Here I post my code.

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]))

Here is subset of my csv file

time         g
6:15:00   141
9:00:00   0
9:25:00   95
9:30:00   0
11:00:00  149
  • What is the expected output? – akilat90 Nov 28 '18 at 05:08
  • @akilat90 I want read my time in this format. as an example 6:15:00 in format %H:%M:%S –  Nov 28 '18 at 05:21
  • Sorry, I'm not clear. 6:15:00 is already in %H:%M:%S format, isn't it? – akilat90 Nov 28 '18 at 05:31
  • @akilat90 Sorry I just put only time column. I edit my question. Here I want to plot graph time with g. to do plot I want to call time in this format. But I can't read it in this format. That's why I tried to convert time. –  Nov 28 '18 at 05:41

2 Answers2

3

Somewhere in your code (or on the command line) you created a global list time (see time.append(...)). This list shadows the module time that you intended to use in your function. In other words: time is a list. Give that list another name.

DYZ
  • 55,249
  • 10
  • 64
  • 93
0

Does this achieve what you want?

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

# plotting
df.plot()
akilat90
  • 5,436
  • 7
  • 28
  • 42