You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas中将含时分秒的datetime列转为仅含年月日的date类型?

Convert Pandas Datetime Column to Date-Only Column

Hey there! Looks like you need to strip the time part from your date_utc datetime column and get a clean date-only column date. Let me walk you through a few simple, reliable ways to do this in Pandas:

Method 1: Get Python date objects

If you want the result to be Python's standard datetime.date type (e.g., datetime.date(2020, 12, 1) which displays as 2020-12-01), use the dt.date accessor:

df['date'] = df['date_utc'].dt.date

This gives you a column of native Python date objects, perfect if you need to work with Python's built-in date functions.

Method 2: Keep Pandas datetime64 type (time set to midnight)

If you want to retain Pandas' datetime64 type but remove the time component (setting it to 00:00:00), you have two solid options:

Option 2a: Use dt.normalize()

This method resets the time part to midnight while keeping the datetime64 type:

df['date'] = df['date_utc'].dt.normalize()

Option 2b: Use dt.floor('D')

floor('D') rounds down the datetime to the nearest day boundary, which also sets the time to midnight:

df['date'] = df['date_utc'].dt.floor('D')

Both of these methods are ideal if you plan to do further time-series operations with the column, as they maintain Pandas' optimized datetime type.

Method 3: Cast to date precision with astype()

You can also directly cast the column to a datetime64 type with day precision:

df['date'] = df['date_utc'].astype('datetime64[D]')

This achieves the same result as Method 2, giving you a datetime64 column with only the date component visible.

Quick Tip

  • Go with Method 1 if you need native Python date objects.
  • Choose Methods 2 or 3 if you want to keep using Pandas' datetime-specific functions (like resampling, filtering by date ranges, etc.) down the line.

内容的提问来源于stack exchange,提问作者mk2080

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:52:18