Python Pandas Select Index where index is larger than x Python Pandas Select Index where index is larger than x pandas pandas

Python Pandas Select Index where index is larger than x


Example of selecting from a DataFrame with the use of index:

from numpy.random import randnfrom pandas import DataFramefrom datetime import timedelta as tdimport dateutil.parserd = dateutil.parser.parse("2014-01-01")df = DataFrame(randn(6,2), columns=list('AB'), index=[d + td(days=x) for x in range(1,7)])In [1]: dfOut[1]:                   A         B2014-01-02 -1.172285  1.7062002014-01-03  0.039511 -0.3207982014-01-04 -0.192179 -0.5393972014-01-05 -0.475917 -0.2800552014-01-06  0.163376  1.1246022014-01-07 -2.477812  0.656750In [2]: df[df.index > dateutil.parser.parse("2014-01-04")]Out[2]:                   A         B2014-01-05 -0.475917 -0.2800552014-01-06  0.163376  1.1246022014-01-07 -2.477812  0.656750


The existing answer is correct, however if we are selecting based on the index, the second method from here would be faster:

# Set indexdf = df.set_index(df['date'])# Select observations between two datetimesdf.loc[pd.TimeStamp('2002-1-1 01:00:00'):pd.TimeStamp('2002-1-1 04:00:00')]