Slicing by partial dates is one of pandas' most convenient features for time-series data — you can specify just a year, year-month, or even a partial date string, and pandas automatically selects the full matching range. This makes it incredibly fast to zoom into months, quarters, years, or custom periods without writing complex filters.
In 2026, partial date slicing remains a go-to technique for quick analysis, reporting, and backtesting. Here’s a practical guide with real examples you can copy and adapt.
1. Setup: Create and Prepare Time-Series Data
import pandas as pd
# Create daily data for January 2022
dates = pd.date_range(start='2022-01-01', end='2022-01-31', freq='D')
df = pd.DataFrame({'value': range(31)}, index=dates)
# Ensure index is sorted datetime (best practice)
df = df.sort_index()
print(df.head())
Output (first few rows):
value
2022-01-01 0
2022-01-02 1
2022-01-03 2
2022-01-04 3
2022-01-05 4
2. Basic Partial Date Slicing
Pass a partial date string — pandas expands it intelligently.
# All of January 2022 (partial year-month)
jan_2022 = df.loc['2022-01']
print(jan_2022.head())
Output (full month):
value
2022-01-01 0
2022-01-02 1
2022-01-03 2
2022-01-04 3
2022-01-05 4
# All of 2022 (just year)
all_2022 = df.loc['2022']
# All data from 2022-01-10 to end of January
mid_to_end_jan = df.loc['2022-01-10':'2022-01']
3. More Flexible Partial Slicing
Quarter or year range
# All Q1 2022 (first three months)
q1_2022 = df.loc['2022-01':'2022-03']
Partial end date (open-ended)
# From Jan 15, 2022 to latest available
from_mid_jan = df.loc['2022-01-15':]
Using datetime objects (more precise)
from datetime import datetime
start = datetime(2022, 1, 10)
end = datetime(2022, 1, 20)
sliced_dt = df.loc[start:end]
4. Real-World Use Cases (2026 Examples)
Monthly reporting
jan_sales = df.loc['2025-01'] # all January 2025
monthly_total = jan_sales.sum()
Compare Q1 across years
q1_all = df.loc['2024-01':'2025-03'] # Q1 2024 and Q1 2025
q1_by_year = q1_all.resample('YS').sum()
Recent data extraction
recent = df.loc['2025-12':] # December 2025 onward
5. Modern Alternative in 2026: Polars
For large time-series datasets, Polars is often faster and more memory-efficient — partial date slicing works via expressions.
import polars as pl
df_pl = pl.DataFrame({
'date': pl.date_range(
start=pl.date(2022, 1, 1),
end=pl.date(2022, 1, 31),
interval='1d'
),
'value': range(31)
}).set_sorted('date')
# All of January 2022 (partial date filter)
jan_pl = df_pl.filter(pl.col('date').dt.strftime('%Y-%m') == '2022-01')
print(jan_pl)
Best Practices & Common Pitfalls
- Always sort the index first:
df = df.sort_index()— partial date slicing requires monotonic index - Both start and end are inclusive — unlike normal Python slicing
- Use string dates or datetime objects — pandas parses intelligently (e.g., '2022-01' ? full month)
- Check index type:
df.index.dtypeshould be datetime64[ns] - Avoid partial strings with ambiguous formats — use ISO 'YYYY-MM-DD' when in doubt
- For huge data, prefer Polars date filtering — it's faster and more memory-efficient
Conclusion
Slicing by partial dates with .loc['YYYY-MM'] or .loc['YYYY'] is one of pandas' smartest time-series features — it automatically expands to full periods without extra code. In 2026, always sort your datetime index first, use ISO strings or datetime objects for clarity, and reach for Polars when scale matters. Master partial date parsing, inclusive ranges, and index sorting, and you'll slice any time window with speed and confidence.
Next time you need a full month, quarter, or year — slice by partial date first. It’s simple, powerful, and saves hours of manual filtering.