Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a Row from a Time Indexed Dataframe

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.

:-)

like image 594
Markus W Avatar asked May 17 '13 15:05

Markus W


People also ask

How do I delete a row in a DataFrame using index?

You can delete a list of rows from Pandas by passing the list of indices to the drop() method. In this code, [5,6] is the index of the rows you want to delete. axis=0 denotes that rows should be deleted from the dataframe.

How do I delete a specific row in a DataFrame?

To drop a specific row from the data frame – specify its index value to the Pandas drop function. It can be useful for selection and aggregation to have a more meaningful index. For our sample data, the “name” column would make a good index also, and make it easier to select country rows for deletion from the data.

How do I delete a row by index?

Delete a Multiple Rows by Index Position in DataFrame As df. drop() function accepts only list of index label names only, so to delete the rows by position we need to create a list of index names from positions and then pass it to drop().

How do I drop a row using ILOC?

Use iloc to get the row as a Series, then get the row's index as the 'name' attribute of the Series. Then use the index to drop.


2 Answers

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')
like image 157
Andy Hayden Avatar answered Oct 31 '22 14:10

Andy Hayden


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)
like image 25
Rens Avatar answered Oct 31 '22 16:10

Rens