---
title: pandas to_datetime — แปลง string วันที่ให้เป็น datetime จริงๆ
url: https://www.thepexcel.com/functions/python/transform/pandas-to-datetime/
type: function-explainer
program: Python
syntax: "pd.to_datetime(arg, format, errors, dayfirst, yearfirst, utc, unit, origin, cache)"
date: 2026-05-31
scores:
  popularity: 5
  difficulty: 3
  usefulness: 5
---

# pandas to_datetime — แปลง string วันที่ให้เป็น datetime จริงๆ

> pd.to_datetime() ใช้แปลงข้อมูลวันที่ที่อยู่ในรูป string หรือตัวเลขให้กลายเป็น datetime ที่ pandas จั

## คำอธิบาย

pd.to_datetime() ใช้แปลงข้อมูลวันที่ที่อยู่ในรูป string หรือตัวเลขให้กลายเป็น datetime ที่ pandas จัดการได้ครับ พอแปลงแล้วก็เรียงลำดับ คำนวณระยะเวลา และดึงเดือน-ปี-วันออกมาได้ทันที

## Syntax

```excel
pd.to_datetime(arg, format, errors, dayfirst, yearfirst, utc, unit, origin, cache)
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| arg | Yes | str \| list \| Series \| DataFrame |  | ข้อมูลที่ต้องการแปลง อาจเป็น string เดี่ยว, list, หรือ Series ของ string วันที่ก็ได้ครับ |
| format | No | str | None | รูปแบบ strftime ที่บอกว่า string นั้นหน้าตาเป็นยังไง เช่น '%d/%m/%Y' หรือ '%Y%m%d' ถ้าไม่ใส่ pandas จะ infer เองครับ |
| errors | No | str | 'raise' | ถ้าแปลงไม่ได้จะทำยังไง: 'raise' = ขึ้น error, 'coerce' = ใส่ NaT แทน ผมชอบ 'coerce' สำหรับข้อมูลสกปรกครับ |
| dayfirst | No | bool | False | ถ้า True ให้ parse วันเป็นตัวแรก เช่น '10/11/2024' = 10 พฤศจิกา ไม่ใช่ 11 ตุลา ใช้กับรูปแบบยุโรป/ไทยครับ |
| yearfirst | No | bool | False | ถ้า True ให้ parse ปีเป็นตัวแรก ใช้กับรูปแบบ ISO เช่น '2024-12-31' (ปกติ pandas ก็ infer ได้เองแต่ระบุชัดกว่าครับ) |
| utc | No | bool | False | ถ้า True จะ normalize ทุก datetime ให้เป็น UTC เหมาะกับข้อมูลที่มีหลาย timezone ผสมกันครับ |
| unit | No | str | None | หน่วยของ arg เมื่อเป็นตัวเลข เช่น 's' = วินาที, 'ms' = millisecond, 'D' = วัน นับจาก origin |
| origin | No | str \| Timestamp | 'unix' | จุดเริ่มต้นสำหรับตัวเลข unit เช่น 'unix' = 1970-01-01, 'julian' หรือ Timestamp ก็ได้ครับ |
| cache | No | bool | True | ถ้า True จะ cache ผลการแปลง string เพื่อความเร็ว ใช้ได้ดีกับข้อมูลที่มีวันที่ซ้ำหลายแถวครับ |

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: แปลง Series ของ string 'YYYY-MM-DD' ให้เป็น datetime

```excel
pd.to_datetime(df['date_str'])
```

**ผลลัพธ์:** `0   2024-01-15
1   2024-02-20
2   2024-03-05
Name: date_str, dtype: datetime64[us]`

ผมส่ง Series ของ string วันที่มาตรฐาน ISO ('YYYY-MM-DD') เข้าไปครับ pandas แปลงให้เป็น datetime64 อัตโนมัติเลยโดยไม่ต้องระบุ format= พอแปลงแล้วคอลัมน์นี้จะเรียงลำดับได้ถูกต้องและคำนวณวันต่างกันได้ทันที

### 2. ตัวอย่างที่ 2: แปลง string รูปแบบ DD/MM/YYYY ด้วย format=

```excel
pd.to_datetime(df['date_str'], format='%d/%m/%Y')
```

**ผลลัพธ์:** `0   2024-01-31
1   2024-02-28
2   2024-03-15
Name: date_str, dtype: datetime64[us]`

รูปแบบ DD/MM/YYYY แบบนี้ pandas ไม่ได้ infer ได้เสมอไปครับ ผมจึงระบุ format='%d/%m/%Y' ให้ชัดเจน ช่วยให้แปลงได้ถูกต้องและเร็วกว่าด้วย เพราะไม่ต้องลองเดารูปแบบ — สำคัญมากกับข้อมูลไทยที่มักใช้รูปแบบนี้ครับ

### 3. ตัวอย่างที่ 3: ดึงปีและเดือนออกมาด้วย .dt accessor

```excel
df['date'].dt.year
```

**ผลลัพธ์:** `0    2024
1    2024
2    2024
Name: date, dtype: int32`

พอแปลงเป็น datetime แล้ว ผมใช้ .dt.year ดึงปีออกมาเป็น Series ตัวเลขได้ทันทีครับ ทำแบบเดียวกันได้กับ .dt.month, .dt.day, .dt.dayofweek, .dt.quarter และอีกเยอะมาก เหมือนกับการแยกคอลัมน์ปี-เดือน-วันใน Excel YEAR()/MONTH() เลยครับ

### 4. ตัวอย่างที่ 4: จัดการค่าผิดพลาดด้วย errors='coerce'

```excel
pd.to_datetime(df['date_str'], errors='coerce')
```

**ผลลัพธ์:** `0   2024-01-15
1          NaT
2   2024-03-05
3          NaT
Name: date_str, dtype: datetime64[us]`

ข้อมูลจริงมักมีค่าผิดพลาดแทรกอยู่ครับ ผมใช้ errors='coerce' เพื่อบอกว่าถ้าแปลงไม่ได้ก็ใส่ NaT (Not a Time) แทนแทนที่จะ raise error ทำให้โปรแกรมทำงานต่อได้ แล้วค่อยกรองหรือ fillna ทีหลังตามความเหมาะสมครับ

## หมายเหตุเพิ่มเติม

- 💡 ถ้าแปลงแล้วยังได้ dtype object อยู่ ลองเพิ่ม format= ให้ชัดขึ้นครับ เพราะบางครั้ง pandas ไม่ infer รูปแบบที่ผิดปกติได้ถูกต้อง โดยเฉพาะ string ที่ผสม timezone หรือ microsecond อยู่ด้วย

- ผมมักเพิ่ม pd.to_datetime() ไว้ใน pipeline ตั้งแต่ตอน read_csv เลยครับ เช่น pd.read_csv('file.csv', parse_dates=['date_col']) ซึ่งเรียก to_datetime ให้อัตโนมัติ ไม่ต้องแปลงทีหลังให้ยุ่งยาก

## คำถามที่พบบ่อย

**Q: ทำไมต้องแปลงเป็น datetime ก่อน ใช้ string ตรงๆ ไม่ได้เหรอ?**

ใช้ได้บางอย่างครับ แต่พังหลายจุดมากเลย 😅 ปัญหาหลักคือการเรียงลำดับ string วันที่แบบ '2/1/2024' < '10/1/2024' ไม่ถูกต้อง (เพราะ '2' > '1' ใน string) และที่สำคัญคือทำ date arithmetic ไม่ได้ เช่น หาว่าสองวันต่างกันกี่วัน ผมเจอ bug แบบนี้บ่อยมากตอนยังใหม่อยู่ครับ แปลงก่อนเสมอดีกว่า

**Q: format='%d/%m/%Y' กับ dayfirst=True ต่างกันยังไง?**

format= บอก pattern ชัดเจน 100% ว่าแต่ละส่วนอยู่ตรงไหนครับ แปลงได้แน่นอนและเร็วกว่า ส่วน dayfirst=True แค่ hint ให้ pandas เวลา infer อัตโนมัติ ซึ่งอาจ override ได้ถ้าหน้าตาข้อมูลมันดูเหมือน month-first มากกว่า ผมแนะนำให้ระบุ format= ตรงๆ เลยถ้าทราบรูปแบบ ไม่ต้องพึ่ง dayfirst ครับ

**Q: ผลลัพธ์เป็น DatetimeIndex บางทีแต่บางทีเป็น Series ทำไม?**

ขึ้นอยู่กับ input ครับ ถ้าส่ง list หรือ array เข้าไปจะได้ DatetimeIndex กลับมา แต่ถ้าส่ง Series เข้าไปจะได้ Series กลับมา ในทางปฏิบัติผมมักใช้กับ Series (df['date_col']) ซึ่งได้ Series datetime64 กลับมา แล้ว assign กลับเข้าคอลัมน์ตามปกติเลยครับ

## แหล่งข้อมูลเพิ่มเติม

- [pandas.to_datetime (official docs)](https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html) _(article)_

---

_Source: [https://www.thepexcel.com/functions/python/transform/pandas-to-datetime/](https://www.thepexcel.com/functions/python/transform/pandas-to-datetime/)_
