Pandas: Extracting Multiple Rows From Dataframe With List Of Datetimeindices
I have a pandas Dataframe with the following index with seconds frequency: DatetimeIndex(['2015-12-28 05:20:05', '2015-12-28 05:20:06', '2015-12-28 05:20:07', '2
Solution 1:
You can convert list
to datetime
by DatetimeIndex
or to_datetime
:
d = ['2015-12-28 05:20:05', '2015-12-28 21:19:58']
print (df.loc[pd.DatetimeIndex(d)])
Or:
print (df.loc[pd.to_datetime(d)])
Sample:
idx = pd.DatetimeIndex(['2015-12-28 05:20:05', '2015-12-28 05:20:06',
'2015-12-28 05:20:07', '2015-12-28 05:20:08',
'2015-12-28 21:19:55', '2015-12-28 21:19:56',
'2015-12-28 21:19:57', '2015-12-28 21:19:58'])
df = pd.DataFrame({'s': range(8)}, index=idx)
print (df)
s
2015-12-28 05:20:05 0
2015-12-28 05:20:06 1
2015-12-28 05:20:07 2
2015-12-28 05:20:08 3
2015-12-28 21:19:55 4
2015-12-28 21:19:56 5
2015-12-28 21:19:57 6
2015-12-28 21:19:58 7
d = ['2015-12-28 05:20:05', '2015-12-28 21:19:58']
print (df.loc[pd.to_datetime(d)])
s
2015-12-28 05:20:05 0
2015-12-28 21:19:58 7
print (df.loc[pd.to_datetime(d)])
s
2015-12-28 05:20:05 0
2015-12-28 21:19:58 7
Post a Comment for "Pandas: Extracting Multiple Rows From Dataframe With List Of Datetimeindices"