从Python/Pandas中的时间索引数据帧中删除行

Mar*_*s W 5 time-series delete-row dataframe pandas

我想通过简单地传递日期和时间来删除Python Dataframe中的一行.

Dataframe具有以下结构:

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
Run Code Online (Sandbox Code Playgroud)

我一直在努力 df = df.drop('2012-01-01 01:00:00')

但我不断收到以下错误消息:

exceptions.ValueError: labels [2012-01-01 01:00:00] not contained in axis
Run Code Online (Sandbox Code Playgroud)

任何有关删除行或只是删除值的帮助将非常感激.

:-)

And*_*den 11

看起来你必须实际使用Timestamp而不是字符串:

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
Run Code Online (Sandbox Code Playgroud)

假设DateTime是索引,如果不使用

df1 = df.set_index('Date_Time')
Run Code Online (Sandbox Code Playgroud)