แยกวิเคราะห์สตริงเป็นวันเวลา
การใช้ to_datetime และการจัดการรูปแบบ
แยกวิเคราะห์สตริงเป็นวันเวลา เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Dates Hide as Text
When you load a CSV, dates usually arrive as plain strings. They look right but pandas treats them as text, so date math just won't work yet. 📅
Enter to_datetime
The fix is one function: pd.to_datetime. It scans your text and turns each value into a real timestamp pandas can compute with.
import pandas as pd
ts = pd.to_datetime("2024-03-15")
print(ts)Convert a Whole Column
Pass a Series and you get back a column of timestamps. This is the everyday move right after loading a table with a date field.
df["date"] = pd.to_datetime(df["date"])Check the dtype
After converting, the column's dtype becomes datetime64. That single check tells you the parse actually worked and date math is now unlocked.
print(df["date"].dtype) # datetime64[ns]Many Formats, One Call
to_datetime is smart: it reads ISO dates, slashes, and even written months automatically. For tidy ISO strings you rarely need to tell it anything else.
pd.to_datetime(["2024-01-01", "Jan 2, 2024"])Spell Out the Format
When dates are ambiguous, give an explicit format string. It removes guessing and runs much faster on big columns.
pd.to_datetime("15/03/2024", format="%d/%m/%Y")Day-First Dates
Is 03/04 March or April? Set dayfirst=True so pandas reads the day before the month, matching most European date styles.
pd.to_datetime("03/04/2024", dayfirst=True)Bad Values Without Crashing
One messy cell can break a whole parse. Use errors="coerce" to turn unparseable values into NaT instead of raising an error.
pd.to_datetime("not-a-date", errors="coerce") # NaTMeet NaT
NaT is the datetime version of NaN: a missing timestamp. You can spot these failed parses with isna and decide how to handle them.
df["date"].isna().sum() # count failed parsesCombine Date and Time Parts
Have separate date and hour columns? Concatenate them into one string, then parse once. You end up with a single clean timestamp.
pd.to_datetime(df["day"] + " " + df["time"])Parse at Read Time
You can skip a step by parsing during import. Pass parse_dates to read_csv and the column arrives as datetimes already.
pd.read_csv("sales.csv", parse_dates=["date"])Quick Check
Your date column has a few junk values you want to keep as missing.
Recap: You Can Parse Dates
You turned text into real timestamps with pd.to_datetime, controlled formats, handled bad values with coerce, and even parsed on load. Date math is now within reach. 🎉
คำถามที่พบบ่อย
บทเรียน “แยกวิเคราะห์สตริงเป็นวันเวลา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แยกวิเคราะห์สตริงเป็นวันเวลา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แยกวิเคราะห์สตริงเป็นวันเวลา”
การใช้ to_datetime และการจัดการรูปแบบ คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “แยกวิเคราะห์สตริงเป็นวันเวลา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แยกวิเคราะห์สตริงเป็นวันเวลา
- แยกปี เดือน และวันในสัปดาห์
- สุ่มตัวอย่างใหม่เป็นรายวันหรือรายเดือน
- เขตเวลาและช่วงวันที่