Truncate / floor a datetime
To truncate a datetime, use dt.floor()
# Generate datetimes precise to the millisecond
df = pd.DataFrame({
'datetime': pd.to_datetime(
np.random.randint(1673897218000, 1681673218000, 5), unit='ms'
)
})
df
For example, to floor to the second, hour or day:
(
df
.assign(
second=lambda x: x['datetime'].dt.floor('s'),
hour=lambda x: x['datetime'].dt.floor('h'),
date=lambda x: x['datetime'].dt.floor('d'),
)
)
Another trick: if you’re converting a string to datetime, and don’t want to be more precise than the second, you can convert the string to datetime64[s]
instead of datetime64
that has a precision up to the nanosecond:
# Convert datetimes to string
df['datetime'] = df['datetime'].astype('str')
# Convert to datetime only precise to the second
df['datetime'].astype('datetime64[s]')
0 2023-03-17 20:34:47
1 2023-04-04 17:47:19
2 2023-03-09 01:51:14
3 2023-02-27 16:20:03
4 2023-03-29 21:04:24
Name: datetime, dtype: datetime64[ns]
Change frequency of a date
To cast a date or datetime to a less granular frequency, use dt.to_period()
with a specified offset, for example:
D
: calendar dayW
: weekM
: monthQ
: quarterY
: year
# Truncate date to month
df['date_converted'].dt.to_period('M')
0 2020-01
1 2020-02
2 2020-03
3 2020-04
4 2020-05
Name: date_converted, dtype: period[M]
# Truncate date to quarter
df['date_converted'].dt.to_period('Q')
0 2020Q1
1 2020Q1
2 2020Q1
3 2020Q2
4 2020Q2
Name: date_converted, dtype: period[Q-DEC]