1

I have a pandas dataframe where one of the columns is made up of strings representing dates, which I then convert to python timestamps by using pd.to_datetime().

How can I select the rows in my dataframe that meet conditions on date.

I know you can use the index (like in this question) but my timestamps are not unique.

How can I select the rows where the 'Date' field is say, after 2015-03-01?

Community
  • 1
  • 1
Alexis Eggermont
  • 7,665
  • 24
  • 60
  • 93

1 Answers1

2

You can use a mask on the date, e.g.

df[df['date'] > '2015-03-01']

Here is a full example:

>>> df = pd.DataFrame({'date': pd.date_range('2015-02-15', periods=5, freq='W'),
                       'val': np.random.random(5)})
>>> df
        date       val
0 2015-02-15  0.638522
1 2015-02-22  0.942384
2 2015-03-01  0.133111
3 2015-03-08  0.694020
4 2015-03-15  0.273877

>>> df[df.date > '2015-03-01']
        date       val
3 2015-03-08  0.694020
4 2015-03-15  0.273877
jakevdp
  • 77,104
  • 11
  • 125
  • 160