6

I have pandas Dataframe with datetime index like 'YYYY-MM-DD HH:MM:SS'.

Index               Parameter
2007-05-02 14:14:08     134.8
2007-05-02 14:14:32     134.8 
2007-05-02 14:14:41     134.8 
2007-05-02 14:14:53     134.8 
2007-05-02 14:15:01     134.8 
2007-05-02 14:15:09     134.8 
......
2007-05-30 23:08:02     105.9 
2007-05-30 23:18:02     105.9 
2007-05-30 23:28:02     105.9 
2007-05-30 23:38:03     105.8 

It is possible to get slice a DataFrame by year df['2007'] or by month df['2007-05']?

But when I've tried to slice DataFrame by day, for example df['2007-05-02'], I've got the error:

KeyError: < Timestamp: 2007-02-05 00:00:00. 

I use the pandas version 8.0.1. Is it possible to slice DataFrame with smaller frequency than year or month? For example, by day or hour?

bmu
  • 35,119
  • 13
  • 91
  • 108
Vitali Molchan
  • 203
  • 4
  • 9
  • 1
    Interesting, indexing by year or year-month has different behavior than by year-month-day. For the year-month-day, only timestamps with H:M:S=0:0:0 are considered. BTW i suppose you are working on a Series and not a DataFrame. – Wouter Overmeire Oct 16 '12 at 12:20

1 Answers1

12

use df.ix[x:y] where x and y are datetime objects.

Example:

In [117]: frame.index.summary()
Out[117]: 'DatetimeIndex: 6312960 entries, 2000-04-05 00:01:00 to 2012-04-06 00:00:00\nFreq: T'


In [118]: x=datetime(2001, 4, 5, 0, 1)

In [119]: y=datetime(2001, 4, 5, 0, 5)

In [120]: print frame.ix[x:y]
                     radiation      tamb
2001-04-05 00:01:00  67.958873  8.077386
2001-04-05 00:02:00  50.801294  0.731453
2001-04-05 00:03:00  16.042035  6.944998
2001-04-05 00:04:00   5.678343  9.728967
2001-04-05 00:05:00  72.551601  7.652942

you can also do this:

In [121]: print frame.ix[x]
radiation    67.958873
tamb          8.077386
Name: 2001-04-05 00:01:00
root
  • 76,608
  • 25
  • 108
  • 120
  • 1
    yes, but can it be done without "hitting" an exact index point? E.g. if the series starts with 2007-05-02 14:14:08, indexing with `x = pd.Timestamp('2007-05-02 14:00:00')` still raises a `KeyError`. – metakermit May 13 '14 at 11:33