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?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I did not realize you could simply use a string as a condition on a timestamp column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.