I am importing event info from csv files and I'm having trouble with the event time. Event times are 12-hour time: hours:minutes am/pm. I converted the strings to datetime objects because the sort function wasn't working with the times as strings using lambda
.
Next, I drop the date info (that's in another column) using pd.DatetimeIndex
.
What's left is 24 hour time, hours:minutes:seconds. I'm trying to convert the time to 12-hour time, hours:minutes am/pm.
from datetime import datetime
for csvfile in glob.glob(csvfiles):
filename = csvfile
df = pd.read_csv(filename)
df = df.fillna('')
df = df.astype(str)
#convert time string to datetime object so times can be sorted
df['MEETING START TIME'] = df['MEETING START TIME'].map(lambda x: pd.datetools.parse(x))
#removing month day year and leaving only time
df['MEETING START TIME'] = pd.DatetimeIndex(df['MEETING START TIME']).time
df['MEETING START TIME'] = datetime.strptime(df['MEETING START TIME'], "%H:%M:%S")
df['MEETING START TIME'].strftime("%I:%M %p")
df = df.sort('MEETING START TIME')
This code results in an error "must be string, not series" for line 53: df['MEETING START TIME'] = datetime.strptime(df['MEETING START TIME'], "%H:%M:%S")
I added the following code
for i in range(len(df['MEETING START TIME'])):
df['MEETING START TIME']=datetime.strptime(df['MEETING START TIME'][i],"%I:%M %p")
print(df['MEETING START TIME'])
I get an error message "Keyerror 0L"
Now I'm really stuck. Any ideas?