Python Forum
Difficulty Using .rstrip in a series
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difficulty Using .rstrip in a series
#1
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
Reply
#2
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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020