0Pricing
Data Science Academy · درس

تحليل السلاسل إلى Datetimes

استخدام to_datetime والتعامل مع التنسيقات

تحليل السلاسل إلى Datetimes درس مجاني في Data Science Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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")  # NaT

Meet 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 parses

Combine 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. 🎉

الأسئلة الشائعة

هل درس «تحليل السلاسل إلى Datetimes» مجاني؟

نعم — نص درس «تحليل السلاسل إلى Datetimes» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Data Science Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Data Science Academy 4 دروس في المجموع.

ماذا ستتعلم في «تحليل السلاسل إلى Datetimes»؟

استخدام to_datetime والتعامل مع التنسيقات تتمرن على Data Science Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Data Science Academy؟

لا تُشترط خبرة سابقة. Data Science Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «تحليل السلاسل إلى Datetimes»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Data Science Academy هذا؟

نعم. كل درس في Data Science Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تحليل السلاسل إلى Datetimes
  2. استخراج السنة والشهر ويوم الأسبوع
  3. إعادة أخذ العينات يوميًا أو شهريًا
  4. المناطق الزمنية ونطاقات التاريخ
← العودة إلى Data Science Academy