Slicing by Partial Dates in Pandas – Year, Month, Quarter & Week 2026
Partial date slicing allows you to easily select data for an entire year, month, quarter, or week without specifying full start and end dates. In 2026, Pandas makes this extremely convenient and efficient when you have a sorted DatetimeIndex.
TL;DR — Partial Date Slicing Syntax
df.loc["2026"]→ entire year 2026df.loc["2026-03"]→ entire March 2026df.loc["2026Q1"]→ first quarter of 2026df.loc["2026-03-15":"2026-03-21"]→ specific week
1. Basic Partial Date Slicing
import pandas as pd
# Create a small demo file so this example runs as-is
# (in your project, sales_data.csv is your real export)
import numpy as np
rng = np.random.default_rng(42)
n = 60
pd.DataFrame({
"order_date": pd.date_range("2026-01-01", periods=n, freq="3D"),
"customer_id": rng.integers(101, 121, n),
"customer_name": rng.choice([" ana", "ben ", "chen", "dana"], n),
"product_id": rng.integers(1, 9, n),
"region": rng.choice(["North", "South", "East", "West"], n),
"category": rng.choice(["Electronics", "Computers", "Books", "Home"], n),
"payment_method": rng.choice(["card", "cash", "wallet"], n),
"segment": rng.choice(["consumer", "business"], n),
"quantity": rng.integers(1, 6, n),
"amount": rng.uniform(50, 2500, n).round(2),
"discount": rng.choice([0.0, 0.05, 0.1, 0.2], n),
}).assign(
price=lambda d: (d["amount"] / d["quantity"]).round(2),
profit=lambda d: (d["amount"] * rng.uniform(0.05, 0.3, n)).round(2),
).assign(
ship_date=lambda d: d["order_date"] + pd.Timedelta(days=2),
status=lambda d: np.where(d["amount"] > 2000, "priority", "standard"),
).to_csv("sales_data.csv", index=False)
df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])
df = df.set_index("order_date").sort_index()
# Slice by Year
sales_2026 = df.loc["2026"]
# Slice by Month
march_sales = df.loc["2026-03"]
# Slice by Quarter
quarters = df.index.to_period("Q") # quarter strings in .loc are deprecated in pandas 3
q1_sales = df[quarters == "2026Q1"]
q2_sales = df[quarters == "2026Q2"]
2. Combining Partial Dates with Other Filters
# March 2026 sales for North region only
north_march = df.loc["2026-03"].loc[df["region"] == "North"]
# All sales in 2026 for Electronics category
electronics_2026 = df.loc["2026"].loc[df["category"] == "Electronics"]
3. Advanced Partial Date Slicing
# First half of 2026
first_half = df.loc["2026-01":"2026-06"]
# Specific week using ISO week format
week_12 = df.loc["2026-03-16":"2026-03-22"]
# Last 3 months of 2026
last_quarter = df.loc["2026-10":]
4. Best Practices in 2026
- Always set the date column as index and sort it first with
.sort_index() - Use partial string indexing like
"2026","2026-03", or"2026Q1" - Partial date slicing works only on a sorted DatetimeIndex
- Combine partial date slicing with boolean filtering for powerful queries
- Use
pd.Grouper(freq="ME")orresample()when you need aggregation by time period
Partial Date Strings
A partial date string gives only part of a date, such as "2026" or "2026-02". On a DatetimeIndex, df.loc["2026-02"] returns every row in February, so you do not need to work out the last day of the month yourself.
Quarters and weeks are easier with periods. Converting the index with to_period("Q") or grouping with resample("W") keeps the code clear and avoids string formats that pandas may deprecate.
Worked Example: Month, Range and Quarter
Daily data for the first half of 2026 is selected by month, by a month range, and by quarter.
import pandas as pd
df = pd.DataFrame(
{"sales": 1},
index=pd.date_range("2026-01-01", "2026-06-30", freq="D"),
)
print(len(df.loc["2026-02"]))
print(len(df.loc["2026-02":"2026-03"]))
quarters = df.index.to_period("Q")
print(df[quarters == pd.Period("2026Q2")].shape[0])
print(df.resample("W").sum().head(3))
February 2026 has 28 days, February plus March gives 59 rows, and the second quarter holds 91 days. Weekly resampling sums each calendar week ending on Sunday.
Practical Tips
Partial strings are also handy in reports: a loop over ["2026-01", "2026-02", "2026-03"] can produce one summary per month with a single line of selection code. Check the row count of each slice to spot months with missing days.
Common Mistakes
- Using partial strings on a column instead of the index.
- Assuming every month has the same number of rows.
- Using the old "M" and "Q" frequency aliases removed in pandas 3.
- Forgetting to sort before a partial string range.
Quick Summary
- Partial strings select whole years or months.
- Ranges of partial strings include both end periods.
- Use to_period("Q") for quarter filters.
- Use resample("W") for weekly summaries.
Conclusion
Partial date slicing is one of the most convenient features in Pandas when working with time-series data. In 2026, once you set a sorted DatetimeIndex, you can easily slice by year ("2026"), month ("2026-03"), quarter ("2026Q1"), or any custom range with simple, readable syntax. This technique greatly simplifies time-based data analysis and reporting.
Next steps:
- Set your date column as index, sort it, and practice slicing by year, month, and quarter using partial date strings