Working with dates
Convert text to real dates, pull out months and weekdays, and total by month.
Dates in a CSV arrive as text like "2026-02-14". Text sorts and compares only letter by letter, so convert it with pd.to_datetime. A real date column has a .dt accessor that pulls out parts: .dt.month (1 to 12), .dt.year, .dt.day_name() ("Saturday"). With those parts you can ask time questions: totals per month, busiest weekday, or rows after a certain date.
import io
import pandas as pd
csv_text = """date,visitors
2026-01-04,120
2026-01-11,135
2026-01-25,150
2026-02-01,98
2026-02-14,210
2026-02-22,140
2026-03-07,175
2026-03-15,160
2026-03-28,190
"""
visits = pd.read_csv(io.StringIO(csv_text))
visits["date"] = pd.to_datetime(visits["date"])
visits["month"] = visits["date"].dt.month
visits["weekday"] = visits["date"].dt.day_name()
print(visits.head())
print(visits.groupby("month")["visitors"].sum())
print(visits[visits["date"] >= "2026-03-01"])After to_datetime, the dtype becomes datetime64. The month column lets groupby total each month (Jan 405, Feb 448, Mar 525). Comparing to "2026-03-01" works because pandas understands dates now.
visits["date"].dt.to_period("M") gives labels like 2026-02, which keeps the year and month together. That matters when your data spans more than one year.
Use the ISO format year-month-day in your own data. It is unambiguous: 03/04 could be March 4 or April 3!