# Parts of a Date: .dt

Once a column holds dates, .dt gives its parts for every row at once, such as year, month, day_name(), hour and date, so you can count by weekday or hour, and subtracting two dates gives a Timedelta, the gap between them, whose .dt.days counts whole days.

Apprentice · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Pull parts out of a datetime column with .dt, such as year, month, day_name(), hour and date
- Work out the gap between two dates as a Timedelta, and count rows by weekday in calendar order

## Key takeaways

- .dt, read "dot d t", gives a date column its date tools, as .str gives a text column its string tools. It works only on a datetime64 column.
- .dt.year, .dt.month, .dt.hour and .dt.date take one part of every date, and .dt.day_name() gives the day of the week as a word.
- value_counts() on a part counts the rows for each day or hour, and reindex() puts the days in calendar order.
- Subtracting one date from another gives a Timedelta, the length of time between them, and .dt.days gives it in whole days.

Which day do most orders arrive? What hour is the help desk busiest? How many days before a deadline do people sign up? Questions like these come up in every job that keeps dates, and they're all about one part of a date, or the gap between two. This lesson takes a date column apart.

The example is the Hillcrest 5K Fun Run's registrations, `registrations.csv`: 450 runners and the moment each sent the sign-up form, `registered_at`, such as `2026-08-03 07:40`. The race is on Saturday, October 3, 2026, and the race director is planning next year's advertising: on which days, and at what time, do people sign up? The runners and the race are our own made-up data.

## .dt: the date tools for a whole column

Load the file with `registered_at` read as dates, as *Parsing Dates with to_datetime* shows:

```python
import pandas as pd

reg = pd.read_csv("registrations.csv", parse_dates=["registered_at"])
signed = reg["registered_at"]
signed.head(3)
```

```text
0   2026-08-03 07:40:00
1   2026-08-04 06:27:00
2   2026-08-04 06:49:00
Name: registered_at, dtype: datetime64[us]
```

Each value is a **Timestamp**, pandas' word for one date and time. A date column has a set of tools under **`.dt`** (read "dot d t"), as a text column has `.str`, and each works on every row at once. A few of them take one part of each date:

```python
pd.DataFrame({
    "registered_at": signed,
    "year": signed.dt.year,
    "month": signed.dt.month,
    "day": signed.dt.day_name(),
    "hour": signed.dt.hour,
}).head(3)
```

```text
        registered_at  year  month      day  hour
0 2026-08-03 07:40:00  2026      8   Monday     7
1 2026-08-04 06:27:00  2026      8  Tuesday     6
2 2026-08-04 06:49:00  2026      8  Tuesday     6
```

`year`, `month` and `hour` are numbers, with no brackets, because they're stored values. `day_name()` has brackets because it works out a word for each day. `.dt` works only on a `datetime64` column: on text such as `"2026-08-03 07:40"`, pandas stops with an error that says so.

## Counting by weekday

With the weekday as a word, `value_counts()` counts the registrations for each day:

```python
signed.dt.day_name().value_counts()
```

```text
registered_at
Tuesday      141
Thursday      67
Saturday      64
Wednesday     60
Monday        47
Friday        45
Sunday        26
Name: count, dtype: int64
```

Tuesday stands out, with more than twice any other day. `value_counts` sorts from most to fewest, which is right for "which day is busiest", but a week reads better in calendar order. **`reindex()`** puts the rows in the order of a list you give it, and `fill_value=0` writes 0 for a day that isn't there at all, instead of `NaN`:

```python
week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
signed.dt.day_name().value_counts().reindex(week, fill_value=0)
```

```text
registered_at
Monday        47
Tuesday      141
Wednesday     60
Thursday      67
Friday        45
Saturday      64
Sunday        26
Name: count, dtype: int64
```

## Hours and days

Why Tuesday? Take only Tuesday's registrations and count them by hour:

```python
tuesdays = signed[signed.dt.day_name() == "Tuesday"]
tuesdays.dt.hour.value_counts().head(3)
```

```text
registered_at
20    53
19    25
21    16
Name: count, dtype: int64
```

Most Tuesday sign-ups come between 7 and 10 in the evening. Hillcrest's running club meets on Tuesday evenings, so runners register when they get home from the club run. That's next year's advertising plan: a poster at the club, and a reminder on Tuesday nights.

**`.dt.date`** drops the time and keeps the calendar day, which counts registrations per day:

```python
signed.dt.date.value_counts().head(3)
```

```text
registered_at
2026-08-04    22
2026-09-15    20
2026-09-01    18
Name: count, dtype: int64
```

The three busiest days are all Tuesdays, the first one the day after the form opened.

## The gap between two dates: Timedelta

How long before the race did people sign up? Subtract each registration from race day. Subtracting one date from another gives a **Timedelta**, a length of time, such as "60 days and 16 hours":

```python
race_day = pd.Timestamp("2026-10-03")
before = race_day - signed
before.head(3)
```

```text
0   60 days 16:20:00
1   59 days 17:33:00
2   59 days 17:11:00
Name: registered_at, dtype: timedelta64[us]
```

A Timedelta column has `.dt` tools too. **`.dt.days`** gives the whole days, which makes a question such as "who signed up in the last week?" a comparison:

```python
print("longest wait:", before.max())
print("signed up in the last week:", (before.dt.days < 7).sum())
```

```text
longest wait: 60 days 16:20:00
signed up in the last week: 50
```

Fifty runners signed up in the final week, so next year's form stays open until the night before. A Timedelta also measures how long a run took, and *Durations: Finish Times with Timedelta* uses it for that.

## Check yourself

2 questions to try on the lesson page.

## So what

`.dt` turns a date column into the parts your question is about, a weekday, an hour, a day, and subtracting dates gives a Timedelta you can measure in days, so "when?" and "how long before?" become counts you can act on.

## Key terms

- [dt accessor](https://www.ask-codey.com/glossary/dt-accessor)
- [Timestamp](https://www.ask-codey.com/glossary/timestamp)
- [Timedelta](https://www.ask-codey.com/glossary/timedelta)

## Related

- [Parsing Dates with to_datetime](https://www.ask-codey.com/lessons/pandas-to-datetime)
- [Durations: Finish Times with Timedelta](https://www.ask-codey.com/lessons/pandas-durations)
- [Grouping with groupby](https://www.ask-codey.com/lessons/pandas-groupby)

Source: https://www.ask-codey.com/lessons/pandas-dt-accessor
