13

I'm trying to delete a row in a Pandas dataframe by simply passing the date and time.

The dataframe has the following structure:

Date_Time             Price1   Price2    Price3                       
2012-01-01 00:00:00    63.05    41.40    68.14
2012-01-01 01:00:00    68.20    42.44    59.64
2012-01-01 02:00:00    61.68    43.18    49.81

I have been trying with df = df.drop('2012-01-01 01:00:00')

But I keep getting the following error message:

exceptions.ValueError: labels [2012-01-01 01:00:00] not contained in axis

Any help on either deleting the row or just deleting the values would be much appreciated.

:-)

petezurich
  • 9,280
  • 9
  • 43
  • 57
Markus W
  • 1,451
  • 5
  • 19
  • 32

2 Answers2

19

It looks like you have to actually use the Timestamp rather than the string:

In [11]: df1
Out[11]:
                     Price1  Price2  Price3
Date_Time
2012-01-01 00:00:00   63.05   41.40   68.14
2012-01-01 01:00:00   68.20   42.44   59.64
2012-01-01 02:00:00   61.68   43.18   49.81

In [12]: df1.drop(pd.Timestamp('2012-01-01 01:00:00'))
Out[12]:
                     Price1  Price2  Price3
Date_Time
2012-01-01 00:00:00   63.05   41.40   68.14
2012-01-01 02:00:00   61.68   43.18   49.81

Assuming DateTime is the index, if not use

df1 = df.set_index('Date_Time')
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
2

Alternatively, this works, too:

df1.drop(df1.loc[df1['Date_Time'] == '2012-01-01 01:00:00'].index, inplace=True)

It's also handy when you like to drop a range of observations based on the datetime index. E.g. all observations later than 2012-01-01 01:00:00:

df1.drop(df1.loc[df1['Date_Time'] > '2012-01-01 01:00:00'].index, inplace=True)
Rens
  • 492
  • 1
  • 5
  • 14
  • 1
    Question was deleted, need `df['rev_dis'] = np.where((df['date'] >= '2020-01-02') & (df['date'] <= '2020-01-03'), df['rev'] * 0.5, df['rev'])` – jezrael Mar 16 '21 at 08:24
  • Jezrael! Thanks so much! I was stressing too much over this. Your help is much appreated. I just had to keep the index as it was (so not set the date as index) and it worked! – Rens Mar 16 '21 at 08:31