什么是获取数据的最pythonic方式只有7:00小时

时间:2016-11-17 09:47:49

标签: python datetime pandas numpy dataframe

Date    Y
12/16/2013 7:00 104052
12/16/2013 15:00    103213
12/16/2013 23:00    104724
12/17/2013 7:00 104257
12/17/2013 15:00    105565
12/17/2013 23:00    103970
12/18/2013 7:00 104026
12/18/2013 15:00    103532
12/18/2013 23:00    101313
12/19/2013 7:00 105233
12/19/2013 15:00    105864
12/19/2013 23:00    105621
12/20/2013 7:00 108011
12/20/2013 15:00    108263
12/20/2013 23:00    107320
12/21/2013 7:00 106211
12/21/2013 15:00    106315
12/21/2013 23:00    104821
12/22/2013 7:00 106312
12/22/2013 15:00    107649
12/22/2013 23:00    107690
12/23/2013 7:00 107274
12/23/2013 15:00    107298
12/23/2013 23:00    107059

我已经尝试过这段代码,但我只根据日期获取数据。

fcdata = read_csv("Data_Analysis_Sample.csv", index_col=0)
dateparse = lambda dates: datetime.strptime(dates, '%m/%d/%Y %H:%M')
fcdata = read_csv("Data_Analysis_Sample.csv", parse_dates=True, 
                  index_col="Date", date_parser=dateparse)
ts = fcdata["Y"]

ts["7:00"]

输出:KeyError: '7:00'

1 个答案:

答案 0 :(得分:1)

您需要boolean indexing

print (fcdata.index.hour == 7) & (fcdata.index.minute == 0))
[ True False False  True False False  True False False  True False False
  True False False  True False False  True False False  True False False]

print (fcdata[(fcdata.index.hour == 7) & (fcdata.index.minute == 0)])
                          Y
Date                       
2013-12-16 07:00:00  104052
2013-12-17 07:00:00  104257
2013-12-18 07:00:00  104026
2013-12-19 07:00:00  105233
2013-12-20 07:00:00  108011
2013-12-21 07:00:00  106211
2013-12-22 07:00:00  106312
2013-12-23 07:00:00  107274