Dheeraj Dheeraj - 16 days ago 4
Python Question

what is the most pythonic way to fetch the data of only 7:00 hrs

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


I have tried this code but I am getting the data according to dates only.

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"]


output:
KeyError: '7:00'

Answer

You need 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
Comments