เขตเวลาและช่วงวันที่
กำหนดเขตเวลาและสร้างวันที่
เขตเวลาและช่วงวันที่ เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Time Has a Place
9 a.m. in Tokyo is not 9 a.m. in London. Real-world timestamps need a time zone so they mean the same moment everywhere. 🌍
Naive vs Aware
A timestamp with no zone is called naive. One that knows its zone is aware. Mixing the two leads to silent, painful bugs.
ts = pd.Timestamp("2024-03-15 09:00")
print(ts.tz) # None means naiveAttach a Zone
Use tz_localize to stamp a naive time with its true zone. This says where the clock reading was actually taken.
ts = ts.tz_localize("Asia/Tokyo")Convert Between Zones
Once aware, switch zones with tz_convert. The underlying instant stays fixed; only the displayed clock changes.
ts.tz_convert("Europe/London")Localize, Then Convert
The golden order is localize first, convert second. tz_localize sets the origin zone; tz_convert moves it. Never convert a naive value.
s.dt.tz_localize("UTC").dt.tz_convert("US/Eastern")UTC Is Your Anchor
Store data in UTC and convert only for display. This avoids confusion and sidesteps daylight-saving headaches entirely.
pd.to_datetime(df["ts"], utc=True)Generate a Date Range
Need a clean sequence of dates? date_range builds one from a start, an end, and a frequency in a single call.
pd.date_range("2024-01-01", "2024-01-07")Set the Frequency
The freq argument controls spacing: "D" for daily, "h" for hourly, "ME" for month-end. The same rules you used to resample.
pd.date_range("2024-01-01", periods=12, freq="ME")Count Instead of End
Skip the end date and pass periods to get exactly that many steps. Handy when you know the count but not the finish.
pd.date_range("2024-01-01", periods=30, freq="D")Ranges as an Index
A date range makes a perfect index for a complete time series, exposing missing days you can then fill or flag.
df = df.reindex(pd.date_range("2024-01-01", periods=31))Build Date Features Fast
Pair date_range with the dt accessor to manufacture calendars, fill gaps, or label every day in a forecast horizon.
pd.date_range("2024-01-01", periods=7).day_name()Quick Check
You have a naive timestamp and want to show it in another zone.
Recap: Zones and Ranges
You localized and converted time zones the right way, leaned on UTC as an anchor, and built clean calendars with date_range. ⏰
คำถามที่พบบ่อย
บทเรียน “เขตเวลาและช่วงวันที่” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เขตเวลาและช่วงวันที่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เขตเวลาและช่วงวันที่”
กำหนดเขตเวลาและสร้างวันที่ คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “เขตเวลาและช่วงวันที่” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แยกวิเคราะห์สตริงเป็นวันเวลา
- แยกปี เดือน และวันในสัปดาห์
- สุ่มตัวอย่างใหม่เป็นรายวันหรือรายเดือน
- เขตเวลาและช่วงวันที่