Reputation: 96350
I have a dataframe indexed by datetime
objects:
In <10>: all_data.head().index
Out<10>:
Index([2014-04-23, 2014-04-13, 2014-04-15, 2014-04-30, 2014-04-06], dtype='object')
and two timestamps:
In <11>: d1
Out<11>: datetime.datetime(2014, 3, 24, 0, 0)
In <12>: d2
Out<12>: datetime.datetime(2014, 4, 6, 0, 0)
I would like to index a column base don the d1:d2
range. Note that d1
or d2
may not be in the index. How can I do this in Pandas?
I tried:
all_data.loc[d1:d2,:]
but I get: start bound[2014-03-24 00:00:00] is not the [index]
Upvotes: 3
Views: 1634
Reputation: 13757
Well, if you make the index a DateTimeIndex
, partial string indexing should work:
print df
print df.index
x1 x2
date
2014-04-23 1 2
2014-04-13 2 4
2014-04-15 3 6
2014-04-30 4 8
2014-04-06 5 10
[5 rows x 2 columns]
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-04-23, ..., 2014-04-06]
Then you can use partial string slicing:
print df['2014-03-24':'2014-04-06']
x1 x2
2014-04-06 5 10
or
print df.ix['2014-03-24':'2014-04-13',:]
x1 x2
date
2014-04-13 2 4
2014-04-06 5 10
Upvotes: 3