0

I am stuck on an probably stupid issue but I can't find a way.

In my dataframe I have some dates. I want to replace '3000-01-01' by '2900-01-01'. I tried with simple quotes:

BAB = BAB.replace('3000-01-01','2900-01-01')

double quotes:

BAB = BAB.replace("3000-01-01","2900-01-01")

and also by filtering

BAB[BAB['ADHEND'] == '3000-01-01'] = '2900-01-01'

but not working, like the screenshot shows

enter image description here

Can someone helps me understand why?

Thanks

  • [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/4046632) and [Why should I not upload images of code/data/errors?](//meta.stackoverflow.com/q/285551) – buran Apr 04 '23 at 08:47
  • Does this answer your question? [Replacing column values in a pandas DataFrame](https://stackoverflow.com/questions/23307301/replacing-column-values-in-a-pandas-dataframe) – buran Apr 04 '23 at 08:49
  • Thanks @buran, it gave me another idea: "BAB.loc[BAB.ADHEND == '3000-01-01', 'ADHEND'] = '2900-01-01'" but it still doesn't work – user19725009 Apr 04 '23 at 09:14
  • Try: `df.replace('\s*3000-01-01\s*', '2900-01-01', regex=True)` – Corralien Apr 04 '23 at 09:44

1 Answers1

1

If BAB.replace('\s*3000-01-01\s*', '2900-01-01', regex=True) doesn't work, it's probably because each value is an instance of date:

>>> BBA.loc[493704, 'ADHEND']
datetime.date(3000, 1, 1)

If you have the output above, try:

from datetime import date

df.loc[df['ADHEND'] == date(3000, 1, 1), 'ADHEND'] = date(2900, 1, 1)
Corralien
  • 109,409
  • 8
  • 28
  • 52