使用Pandas read_csv按日期选择行

时间:2018-04-06 16:56:35

标签: python pandas date

我有一个csv文件(列:日期,价格,x,y,z),我想只阅读落入某个时间段的行。我试过这个:

...
abc1 = pandas.read_csv(filesource1, parse_dates=['Date'])
abc2 = abc1.price

startDate = datetime(2014,8,1)
endDate = datetime(2018,3,1)
daterange = pandas.date_range(startDate,endDate)

abc3 = abc2[daterange]
print (abc3.head())

这给了我一个列表,其中只包含日期,价格为“NaN'”。怎么会这样做?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

abc1 = pandas.read_csv(filesource1, parse_dates=['Date'], index_col=['Date'], usecols=['Date', 'Price'])