1

I have a series of times in string format. I want to replace values in but it is not working. I feel like I am missing something simple. What is it?

Code

import pandas as pd
s = pd.Series(["08:05:00", "08:15:00", "08:25:00", "08:55:00", "09:05:00", "09:15:00", "09:25:00"])
print(s.dtype)
s.replace({"07:": "7:", "08:": "8:", "09:": "9:"}, inplace=True) 

The output I get is.

object
0    08:05:00
1    08:15:00
2    08:25:00
3    08:55:00
4    09:05:00
5    09:15:00
6    09:25:00
dtype: object

what I want is:

0    8:05:00
1    8:15:00
2    8:25:00
3    8:55:00
4    9:05:00
5    9:15:00
6    9:25:00
dtype: object

2 Answers 2

1

Try this,

s.str.lstrip('0')

O/P;;

0    8:05:00
1    8:15:00
2    8:25:00
3    8:55:00
4    9:05:00
5    9:15:00
6    9:25:00
dtype: object

lstrip docs: https://pandas.pydata.org/docs/reference/api/pandas.Series.str.lstrip.html

Sign up to request clarification or add additional context in comments.

Comments

0

I found the solution which is in the s.replace line of code. Since the desire is to find beginning character of 0 then use of regex is better. The replacement line of code is below.

import pandas as pd
s = pd.Series(["08:05:00", "08:15:00", "08:25:00", "08:55:00", "09:05:00", "09:15:00", "09:25:00"])
print(s.dtype)
s.replace({"^0": ""}, regex=True, inplace=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.