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?

1 Year
Discussion Span
Last Post by mattrweaver

Thanks for the response! I get an error: "must be string, not datetime.time"


ok progress, thank you. Times that originally displayed as

2    09:30:00
3    14:00:00
4    19:00:00
5    19:00:00
6    14:00:00

are displaying as:

2    09:30 AM
3    02:00 PM
4    07:00 PM
5    07:00 PM
6    02:00 PM

Now, later this code:

date = df["DATE"].irow(0)
    print"Printing date: " + date;
    month,day,year = (int(x) for x in date.split('/'))
weekday = day.strftime("%A")

results in this error:

Traceback (most recent call last):
  File "csvproc_fon_frmt_5.py", line 70, in <module>
TypeError: descriptor 'date' requires a 'datetime.datetime' object but received
a 'int'

Just great, the code as shown will not work as there is no reference to pd, although it isn't difficult to work out that it's pandas. I get the impression that the whole thing is over engineered, but I have no intention of discussing this any further until an entire snippet of what does not work is posted.


You mean class datetime.date where datetime is the module, but as you imported from datetime import datetime, datetime.date is the descriptor date of class datetime. What you can do is

import datetime as dt
day = dt.date(year, month, day)

That did it. In a previous version of the script I had import datetime but switched it to from datetime import datetime in order to convert my date strings into date objects. Thanks for your help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.