0

I have pandas dataframe where DateTime column has dtype as Timestamp and want to change all the years in the date which are 2019 to 2018 (as this are misprint). Following is the sample, I have more columns than these and don't want to affect the corresponding values. This example only has limited rows I have a big dataset with many values as 2019.

DateTime             Object

2018-02-12 10:32:00  A
2018-02-12 10:37:00  B
2018-02-12 10:48:00  C
2018-02-12 11:15:00  D
2019-02-02 12:50:00  E
2019-02-15 09:45:00  F
2018-06-22 07:14:00  G

Output expected:

DateTime             Object

2018-02-12 10:32:00  A
2018-02-12 10:37:00  B
2018-02-12 10:48:00  C
2018-02-12 11:15:00  D
2018-02-02 12:50:00  E
2018-02-15 09:45:00  F
2018-06-22 07:14:00  G
Forscher
  • 63
  • 4
  • it shows error: replace() got an unexpected keyword argument 'year' – Forscher Jun 16 '20 at 12:17
  • I did this: df['DateTime'] = pd.to_datetime(df['DateTime']) df['DateTime'] = df['DateTime'].dt.replace(year=2018) AttributeError: 'DatetimeProperties' object has no attribute 'replace' – Forscher Jun 16 '20 at 12:27
  • other years are also getting replaced for eg 2017 is becoming 2018, I just want to change 2019 to 2018. – Forscher Jun 16 '20 at 12:38
  • hmmm, what is logic for replacement? How is possible distingush what values of years are correct and which need change? – jezrael Jun 16 '20 at 12:40
  • the data is expected to be upto 2018 only, it is written in my problem statement. I am assuming that 2019 is misprint of 2018 – Forscher Jun 16 '20 at 12:43
  • Welcome to Stackoverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jun 16 '20 at 12:43
  • Not understand, missing logic here. There has to be more explained which years are correct and which not. – jezrael Jun 16 '20 at 12:45

0 Answers0