Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slice pandas dataframe based on datetime column

I have a pandas dataframe with a column as datatime that looks like:

data.ts_placed
Out[68]: 
1         2008-02-22 15:30:40
2         2008-03-20 16:56:00
3         2008-06-14 21:26:02
4         2008-06-16 10:26:02
5         2008-06-23 20:41:03
6         2008-07-17 08:02:00
7         2008-10-13 12:47:05
8         2008-11-14 09:20:33
9         2009-02-23 11:24:18
10        2009-03-02 10:29:19

I'd like to slice the dataframe by eliminating all rows before 2009

like image 343
Blue Moon Avatar asked Aug 07 '15 13:08

Blue Moon


People also ask

How do you slice a DataFrame for specific columns?

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.

Can you slice a pandas DataFrame?

Slicing a DataFrame in Pandas includes the following steps:Ensure Python is installed (or install ActivePython) Import a dataset. Create a DataFrame. Slice the DataFrame.


1 Answers

You can use a simple string comparison to compare the values against a year string:

In [63]:
df.loc[df['date'] >= '2009']

Out[63]:
                     date
index                    
9     2009-02-23 11:24:18
10    2009-03-02 10:29:19

Or use the dt attribute to access the year:

In [64]:
df.loc[df['date'].dt.year >= 2009]

Out[64]:
                     date
index                    
9     2009-02-23 11:24:18
10    2009-03-02 10:29:19
like image 132
EdChum Avatar answered Sep 29 '22 14:09

EdChum