Python Forum

Full Version: Difficulty Using .rstrip in a series
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I'm hoping to eventually reformat a series of time values so that they are all in military time. I'm still a few steps away from that, but I'm having trouble with one of the first steps, which is to get rid of the 'AM' or 'PM' value.

import pandas as pd
s = pd.Series(['12:34 AM', '12:57 PM', '14:53', '19:00', '6:33 PM'], index = [1, 2, 3, 4, 5])
for i in s:
    if i[-3:] == ' PM':
        i = i.rstrip(' PM')
s
I expect to find the 'PM values gone, but when I print my series, they are still there. Hoping to figure out what it is that I'm missing. Thank you.

Output:
1 12:34 AM 2 12:57 PM 3 14:53 4 19:00 5 6:33 PM dtype: object
import pandas as pd
s = pd.Series(['12:34 AM', '12:57 PM', '14:53', '19:00', '6:33 PM'], index = [1, 2, 3, 4, 5])
l = []
for i in s:
    i = i.rstrip('PM').rstrip('AM').rstrip(' ')
    l.append(i)
s = pd.Series(l, index = [1, 2, 3, 4, 5])
print(s)