1

df.head()

          HEAT/HOT WATER
created_date    
2015-12-31  169457.0
2016-12-31  155578.0
2017-12-31  152009.0
2018-12-31  153277.0
2019-12-31  76712.0

I would like to delete 2019 from index

I have tried this with no success.

df = df[df['created_date'] != '2019']
Christian Torres
  • 143
  • 1
  • 1
  • 7
  • df = df[df['created_date'].year != 2019].copy() – BENY May 16 '19 at 16:19
  • @WeNYoBen I don't follow? Why would making a copy help in an assignment? – roganjosh May 16 '19 at 16:20
  • @roganjosh check https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas, prevent SettingWithCopyWarning – BENY May 16 '19 at 16:25
  • Well aware of that answer. I'm just not sure how you came to the `.copy()` conclusion from the question – roganjosh May 16 '19 at 16:26
  • @WeNYoBen I agree about the Warning and using .copy() but doesnt that only apply when youre setting the df to a new one? in this case the df is remaining the same. – Christian Torres May 16 '19 at 16:30
  • 1
    @ChristianTorres that is just my coding behavior, everyone have their own coding style , like I always do df.loc[1,:] rather than df.loc[1] – BENY May 16 '19 at 16:32

1 Answers1

3

Convert the index to a series so it can support vectorized datetime operations and check which years are not 2019 using Series.ne

# df.index = pd.to_datetime(df.index)
df[df.index.to_series().dt.year.ne(2019)]

               HEAT/HOTWATER
2015-12-31       169457.0
2016-12-31       155578.0
2017-12-31       152009.0
2018-12-31       153277.0

Or simply df[df.index.year != 2019], as the index is already a DatetimeIndex as pointed out by @piRSquared in the comments

yatu
  • 86,083
  • 12
  • 84
  • 139