看起来像df.drop()没有日期时间索引的工作

时间:2014-07-17 05:41:22

标签: python datetime pandas

看起来df.drop()没有按预期工作(注意索引是DatetimeIndex):

>>> df
                            price  bidvol  askvol
datetime
2014-07-16 14:23:46.653000  34911       0       1
2014-07-16 14:23:53.705000  34909       5       0
2014-07-16 14:23:55.326000  34909       2       0
2014-07-16 14:23:59.539000  34908       1       0
2014-07-16 14:23:59.539000  34908       9       0
2014-07-16 14:24:00.219000  34908       2       0

>>> df = df.drop(df.ix['2014-07-16 14:24':])

>>> df
                             price  bidvol  askvol
datetime
2014-07-16 14:23:46.653000  34911       0       1
2014-07-16 14:23:53.705000  34909       5       0
2014-07-16 14:23:55.326000  34909       2       0
2014-07-16 14:23:59.539000  34908       1       0
2014-07-16 14:23:59.539000  34908       9       0
2014-07-16 14:24:00.219000  34908       2       0

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

date = '2014-07-16 14:24'

删除范围:

df = df[:date] # Drops anything after the date
df = df[date:] # Drops anything before the date

要删除一行:

df = df.drop(Timestamp(date)) # Drops the row indexed by the date

这并不是因为日期不起作用。相反,它似乎不会像切片索引一样自动将字符串转换为日期。

检查docs