0
0
Data Analysis Pythondata~10 mins

Date-based indexing and slicing in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Date-based indexing and slicing
Create DateTimeIndex
Create DataFrame with DateTimeIndex
Select single date or date range
Return subset of DataFrame
Use result for analysis or display
We create a DataFrame indexed by dates, then select data by specifying dates or date ranges to get subsets.
Execution Sample
Data Analysis Python
import pandas as pd
idx = pd.date_range('2023-01-01', periods=5)
df = pd.DataFrame({'val':[10,20,30,40,50]}, index=idx)
subset = df['2023-01-02':'2023-01-04']
print(subset)
This code creates a DataFrame with dates as index and selects rows from Jan 2 to Jan 4.
Execution Table
StepActionCode/ExpressionResult/Output
1Create date range indexpd.date_range('2023-01-01', periods=5)DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'], dtype='datetime64[ns]', freq='D')
2Create DataFrame with date indexpd.DataFrame({'val':[10,20,30,40,50]}, index=idx)DataFrame with 5 rows indexed by dates from 2023-01-01 to 2023-01-05
3Select subset by date rangedf['2023-01-02':'2023-01-04']Rows for 2023-01-02, 2023-01-03, 2023-01-04 with values 20, 30, 40
4Print subsetprint(subset) val 2023-01-02 20 2023-01-03 30 2023-01-04 40
💡 Selection ends after 2023-01-04, slicing includes end date in date-based indexing.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
idxundefinedDatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'])samesamesame
dfundefinedundefinedDataFrame with 5 rows indexed by idxsamesame
subsetundefinedundefinedundefinedDataFrame with rows 2023-01-02 to 2023-01-04same
Key Moments - 2 Insights
Why does the slice df['2023-01-02':'2023-01-04'] include the end date 2023-01-04?
In date-based slicing with pandas, the end date is included unlike normal Python slicing. See execution_table step 3 and 4 where 2023-01-04 row is included.
Can I select a single date like df['2023-01-03']?
Yes, selecting a single date returns the row for that date. This is similar to slicing but returns a single row instead of a DataFrame slice.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'val' for the date 2023-01-03 in the subset?
A20
B30
C40
D50
💡 Hint
Check execution_table row 3 and 4 for the subset values.
At which step is the DataFrame 'df' created with the date index?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table step 2 where df is created.
If you change the slice to df['2023-01-02':'2023-01-03'], how many rows will the subset have?
A2
B1
C3
D4
💡 Hint
Date slicing includes both start and end dates, so count rows between 2023-01-02 and 2023-01-03.
Concept Snapshot
Date-based indexing lets you select rows by dates in a DataFrame.
Use a DateTimeIndex for the DataFrame.
Slice with df['start_date':'end_date'] to get all rows in that range, inclusive.
Selecting a single date returns that row.
This is handy for time series data analysis.
Full Transcript
We start by creating a date range index from January 1 to January 5, 2023. Then, we create a DataFrame with this date index and some values. Next, we select a subset of the DataFrame by slicing from January 2 to January 4. The slice includes the end date, so rows for January 2, 3, and 4 are returned. This subset is printed showing the values for those dates. Date-based slicing in pandas includes the end date, unlike normal Python slicing. You can also select a single date to get one row. This method is useful for working with time series data.