Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice a Pandas Dataframe based on datetime index

This has been bothering me for ages now:

Given a simple pandas DataFrame

>>> df  Timestamp     Col1 2008-08-01    0.001373 2008-09-01    0.040192 2008-10-01    0.027794 2008-11-01    0.012590 2008-12-01    0.026394 2009-01-01    0.008564 2009-02-01    0.007714 2009-03-01   -0.019727 2009-04-01    0.008888 2009-05-01    0.039801 2009-06-01    0.010042 2009-07-01    0.020971 2009-08-01    0.011926 2009-09-01    0.024998 2009-10-01    0.005213 2009-11-01    0.016804 2009-12-01    0.020724 2010-01-01    0.006322 2010-02-01    0.008971 2010-03-01    0.003911 2010-04-01    0.013928 2010-05-01    0.004640 2010-06-01    0.000744 2010-07-01    0.004697 2010-08-01    0.002553 2010-09-01    0.002770 2010-10-01    0.002834 2010-11-01    0.002157 2010-12-01    0.001034 

How do I separate it so that a new DataFrame equals the entries in df for the dates between 2009-05-01 and 2010-03-01

>>> df2  Timestamp     Col1 2009-05-01    0.039801 2009-06-01    0.010042 2009-07-01    0.020971 2009-08-01    0.011926 2009-09-01    0.024998 2009-10-01    0.005213 2009-11-01    0.016804 2009-12-01    0.020724 2010-01-01    0.006322 2010-02-01    0.008971 2010-03-01    0.003911 
like image 234
0000 Avatar asked Apr 17 '18 01:04

0000


People also ask

How do I slice data in Pandas DataFrame?

To slice the columns, the syntax is df. loc[:,start:stop:step] ; where start is the name of the first column to take, stop is the name of the last column to take, and step as the number of indices to advance after each extraction; for example, you can select alternate columns.

How do you slice series Pandas?

slice() method is used to slice substrings from a string present in Pandas series object. It is very similar to Python's basic principal of slicing objects that works on [start:stop:step] which means it requires three parameters, where to start, where to end and how much elements to skip.


2 Answers

If you have set the "Timestamp" column as the index , then you can simply use

df['2009-05-01' :'2010-03-01'] 
like image 118
Siva-Sg Avatar answered Sep 24 '22 06:09

Siva-Sg


IIUC, a simple slicing?

from datetime import datetime df2 = df[(df.Timestamp >= datetime(2009, 05, 01)) &          (df.Timestamp <= datetime(2010, 03, 01))] 
like image 24
rafaelc Avatar answered Sep 24 '22 06:09

rafaelc