If date column is the index, then use .loc for label based indexing or .iloc for positional indexing.
For example:
df.loc['2014-01-01':'2014-02-01']
See details here http://pandas.pydata.org/pandas-docs/stable/dsintro.html#indexing-selection
If the column is not the index you have two choices:
- Make it the index (either temporarily or permanently if it's time-series data)
df[(df['date'] > '2013-01-01') & (df['date'] < '2013-02-01')]
See here for the general explanation
Note: .ix is deprecated.
Answer from Retozi on Stack OverflowIf date column is the index, then use .loc for label based indexing or .iloc for positional indexing.
For example:
df.loc['2014-01-01':'2014-02-01']
See details here http://pandas.pydata.org/pandas-docs/stable/dsintro.html#indexing-selection
If the column is not the index you have two choices:
- Make it the index (either temporarily or permanently if it's time-series data)
df[(df['date'] > '2013-01-01') & (df['date'] < '2013-02-01')]
See here for the general explanation
Note: .ix is deprecated.
Previous answer is not correct in my experience, you can't pass it a simple string, needs to be a datetime object. So:
import datetime
df.loc[datetime.date(year=2014,month=1,day=1):datetime.date(year=2014,month=2,day=1)]
Can you filter pandas dataframes by Day/Month date without year? Writing a generalised function for between date that is year agnostic is proving difficult.
Filtering dataframe by column = today's date
How does one sort by date in a Dataframe?
Filtering by month in Python
I'm looking for a way to only extract rows where the date column is equal to today's date. If today is 4/14/21, I only want the row for that date, etc. I don't want to change the date, I basically want it to return the rows for whichever day it is. Any ideas?
| date | hand |
|---|---|
| 2021-04-14 | two pair |
| 2021-04-15 | flush |
| 2021-04-15 | straight |
| 2021-04-16 | royal flush |
| 2021-04-16 | ace high |
| 2021-04-17 | ace high |