search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
chevron_leftSelecting Data Cookbook
Accessing a single value of a DataFrameAccessing columns of a DataFrame using column labelsAccessing columns of a DataFrame using integer indicesAccessing rows of a DataFrame using integer indicesAccessing rows of a DataFrame using row labelsAccessing the first n rowsAccessing the last n rowsAccessing values of a multi-index DataFrameAdding prefix to column labelsAdding suffix to column labelsConverting two columns into a dictionaryExcluding columns based on typeGetting earliest or latest date from DataFrameGetting every nth rowGetting indexes of rows matching conditionsGetting shortest and longest stringsSelecting a single column as a DataFrameSelecting columns of a DataFrame using regexSelecting last column of DataFrameSelecting rows based on a conditionExtracting values of a DataFrame as a Numpy arrayGetting a list of all the column labelsGetting all columns except oneGetting all duplicate rowsGetting all numeric columns of a DataFrameGetting all unique values of columnsGetting column label of max value in each rowGetting column label of minimum value in each rowGetting columns by data typeGetting columns using integer indexGetting first row value of a columnGetting index of Series where value is TrueGetting integer index of a column using its column labelGetting integer index of rows based on column valuesGetting multiple columnsGetting row with largest index valueGetting row with smallest index valueGetting rows based on multiple column valuesGetting rows except someGetting rows from a DataFrame based on column valuesGetting rows that are not in other DataFrameGetting rows using OR statementGetting rows where column values are of specific lengthGetting rows where value is between two valuesGetting rows where values do not contain substringGetting the column labels of a DataFrameGetting the first columnGetting the index of a DataFrameGetting the length of the longest string in a columnGetting the longest string in a columnGetting the row with the maximum column valueGetting the row with the minimum column valueGetting the shape of a DataFrameGetting number of columns of a DataFrameGetting the total number of rows of a DataFrameGetting the total number of values in a DataFrameMaking column labels all lowercaseMaking column labels all uppercaseRandomly select rows based on a conditionRandomly selecting n columns from a DataFrameRandomly selecting n rows from a DataFrameReassigning column valuesRetrieving DataFrame column values as a NumPy arraySelecting columns that do not begin with certain prefixSelecting columns with certain prefixSelecting n rows with the smallest values for a columnSelecting rows based on datesSelecting rows from a DataFrame whose column values are contained in a listSelecting rows from a DataFrame whose column values are NOT contained in a listSelecting rows from a DataFrame whose column values contain a substringSelecting rows starting with substringSelecting top n rows with the largest values for a columnSplitting DataFrame based on column values
check_circle
Mark as learned
thumb_up
3
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

Selecting rows based on dates in Pandas

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

To select rows based on dates, use DataFrame's query(~) method.

When index is datetime

Consider the following DataFrame with a DatetimeIndex:

index_date = pd.date_range("2020-12-25", "2020-12-28")
df = pd.DataFrame({"A":[2,3,4,5],"B":[6,7,8,9]}, index=index_date)
df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-12-28 5 9

Getting rows using a single date

To get a specific row using dates:

df.query("index == 20201226") # returns a DataFrame
A B
2020-12-26 3 7

Here, we get fetching the row with index 2020-12-26.

You could also use the loc property like so:

df.loc["2020-12-26"] # returns a Series
A 3
B 7
Name: 2020-12-26 00:00:00, dtype: int64

Note that loc property is less flexible than the query(~) method, so for more complex cases like those below, always opt for the query(~) method.

Getting rows using multiple dates

To get a subset of rows, you can pass in a list of row index:

df.query("index==20201225 or index==20201228")
A B
2020-12-25 2 6
2020-12-28 5 9

Getting rows using inequality range

To get rows where the index is "less than" a certain date:

df.query("index <= 20201226")
A B
2020-12-25 2 6
2020-12-26 3 7

Getting rows using date intervals

To get rows where the index is between a certain date interval:

df.query("20201225 < index < 20201227")
A B
2020-12-26 3 7

When a column is datetime

Consider the following DataFrame:

dates = pd.date_range("2020-12-25", "2020-12-28")
df = pd.DataFrame({"A":index_date,"B":[6,7,8,9]})
df
A B
0 2020-12-25 6
1 2020-12-26 7
2 2020-12-27 8
3 2020-12-28 9

If we use the query(~) method, then all you have to do is to replace the word "index" with the desired column label.

Getting rows using a single date

df.query("A == 20201226")
A B
1 2020-12-26 7

Getting rows using multiple dates

df.query("A == 20201225 or A == 20201228")
A B
0 2020-12-25 6
3 2020-12-28 9

Getting rows using inequality range

df.query("A <= 20201226")
A B
0 2020-12-25 6
1 2020-12-26 7

Getting rows using date intervals

df.query("20201225 < A < 20201227")
A B
1 2020-12-26 7
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...