日期替换 Pandas

huangapple go评论67阅读模式
英文:

Date replacement Pandas

问题

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

英文:

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

日期替换 Pandas

Can someone helps me understand why?

Thanks

答案1

得分: 1

If BAB.replace(' 3000-01-01 ', '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)
英文:

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)

huangapple
  • 本文由 发表于 2023年4月4日 16:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定