96

How do I get the min and max Dates from a dataframe's major axis?

           value
Date                                           
2014-03-13  10000.000 
2014-03-21   2000.000 
2014-03-27   2000.000 
2014-03-17    200.000 
2014-03-17      5.000 
2014-03-17     70.000 
2014-03-21    200.000 
2014-03-27      5.000 
2014-03-27     25.000 
2014-03-31      0.020 
2014-03-31     12.000 
2014-03-31      0.022

Essentially I want a way to get the min and max dates, i.e. 2014-03-13 and 2014-03-31. I tried using numpy.min or df.min(axis=0), I'm able to get the min or max value but that's not what I want

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
pyCthon
  • 11,746
  • 20
  • 73
  • 135

3 Answers3

134

'Date' is your index so you want to do,

print (df.index.min())
print (df.index.max())

2014-03-13 00:00:00
2014-03-31 00:00:00
Merlin
  • 24,552
  • 41
  • 131
  • 206
Karl D.
  • 13,332
  • 5
  • 56
  • 38
70
min(df['some_property'])
max(df['some_property'])

The built-in functions work well with Pandas Dataframes.

J.Weed
  • 717
  • 5
  • 3
5

Use agg to determine the minimum and maximum value in one line:

In [5]: df['Date'].agg(['min', 'max'])
Out[5]: 
min    2014-03-13
max    2014-03-31

If your desired column is in the index, you have to reset the index first:

df.reset_index()['Date'].agg(['min', 'max'])
rachwa
  • 1,805
  • 1
  • 14
  • 17