21

I have this simplified dataframe:

ID, Date
1 8/24/1995
2 8/1/1899 :00

How can I use the power of pandas to recognize any date in the dataframe which has extra :00 and removes it.

Any idea how to solve this problem?

I have tried this syntax but did not help:

df[df["Date"].str.replace(to_replace="\s:00", value="")]

The Output Should Be Like:

ID, Date
1 8/24/1995
2 8/1/1899
2
  • Are you creating the data frame yourself? Because you could remove the extraneous :00 prior to making the df. Commented Aug 1, 2016 at 19:40
  • I am reading a .csv file as a dataframe using pd.read.csv()... but I have noticed that some dates do actually have extraneous ` :00` prior to reading them as a dataframe Commented Aug 1, 2016 at 19:45

2 Answers 2

32

You need to assign the trimmed column back to the original column instead of doing subsetting, and also the str.replace method doesn't seem to have the to_replace and value parameter. It has pat and repl parameter instead:

df["Date"] = df["Date"].str.replace("\s:00", "")

df
#   ID       Date
#0   1  8/24/1995
#1   2   8/1/1899
Sign up to request clarification or add additional context in comments.

1 Comment

for regex based expressions don't forget to put regex = True inside replace method
5

To apply this to an entire dataframe, I'd stack then unstack

df.stack().str.replace(r'\s:00', '').unstack()

enter image description here

functionalized

def dfreplace(df, *args, **kwargs):
    s = pd.Series(df.values.flatten())
    s = s.str.replace(*args, **kwargs)
    return pd.DataFrame(s.values.reshape(df.shape), df.index, df.columns)

Examples

df = pd.DataFrame(['8/24/1995', '8/1/1899 :00'], pd.Index([1, 2], name='ID'), ['Date'])

dfreplace(df, '\s:00', '')

enter image description here


rng = range(5)
df2 = pd.concat([pd.concat([df for _ in rng]) for _ in rng], axis=1)

df2

enter image description here

dfreplace(df2, '\s:00', '')

enter image description here

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.