使用pandas datetime在ipython中切片数据

时间:2016-01-27 23:18:45

标签: pandas ipython

我从RBR.csv创建了一个数据帧df2,并希望仅在start:18-04-2015 21:30和End:18-09-2015 14:09之间使用数据设置为dataframe df3

In [12]:

df3 = df2[(df2['date']>=datetime.datetime(18-04-2015 21:30)) & 
          (df2['date'] <= datetime.datetime(18-09-2015 14:09))]

File "<ipython-input-12-95bbb0030f0a>", line 1
    df3 = df2[(df2['date']>=datetime.datetime(18-04-2015 21:30)) &   
(df2['date'] <= datetime.datetime(18-09-2015 14:09))]
                                                      ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

您需要正确创建datetime.datetime个实例:

df3 = df2[(df2['date'] >= datetime.datetime(2015, 4, 18, 21, 30)) & 
          (df2['date'] <= datetime.datetime(2015, 9, 18, 14, 9))]

或者,您可以使用字符串:

df3 = df2[(df2['date'] >= '18-04-2015 21:30') & (df2['date'] <= '18-09-2015 14:09')]