英文:
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
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论