-1

I have some data:

ID Date
1 2021-05-01
2 2021-05-01
3 2021-05-01
4 -99
5 -99
6 2017-11-21
7 -99
8 2019-02-04
9 -99
10 -99

Where I have -99, I want to change it to 2021-05-20. I have tried if statement but it won't work, there are still -99s. I have made sure it is a datetime column although thinking about that -99 isn't a datetime value. Maybe that is where I'm going wrong?

df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')

if (df['Date'] is '-99'):
    df['Date'] = pd.Series(['2021-05-20' for x in range(len(df.index))])

I'm thinking the length part is wrong too, since there aren't as many -99 as there are data rows. Thanks in advance for the help!

  • Did you try [replace](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.replace.html)? – Erfan May 27 '21 at 14:38

1 Answers1

1

A mask option:

df['Date'] = df['Date'].mask(df['Date'].eq('-99'), '2021-05-20')

A np.where option:

df['Date'] = np.where(df['Date'].eq('-99'), '2021-05-20', df['Date'])

A loc option:

df.loc[df['Date'].eq('-99'), 'Date'] = '2021-05-20'

A replace option:

df['Date'] = df['Date'].replace({'-99': '2021-05-20'})

Convert to DateTime Option via pd.to_datetime + fillna credit to @Nk03:

df['Date'] = (
    pd.to_datetime(df['Date'], errors='coerce').fillna(pd.to_datetime('2021-05-20'))
)
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • 2
    You can add one more option - pd.to_datetime with 'coerce' and then fill nan with required values. . – Nk03 May 27 '21 at 14:39