← Back to Pandas Course | Chapter 10: Time Series | Lesson 1 of 7

pd.to_datetime() का उपयोग

to_datetime text या numbers को असली datetime values में बदल देता है जिन पर आप date maths कर सकते हैं।

In this page:

  1. pd.to_datetime()
Syntax
python
df["date"] = pd.to_datetime(df["date"])
pd.to_datetime("2024-03-15")

pd.to_datetime()

pd.to_datetime "2024-03-15" जैसी strings को Timestamp objects में parse करता है, और पूरे column को datetime64 में। non-ISO layouts के लिए format="%d/%m/%Y" और गलत dates को NaT में बदलने के लिए errors="coerce" का उपयोग करें।

फिर .dt accessor year, month, day और weekday उजागर करता है।

Note: NaT, NaN का datetime equivalent है।

उदाहरण: pd.to_datetime()

python
import pandas as pd

s = pd.Series(["2024-03-15", "2024-12-01", "not a date"])
d = pd.to_datetime(s, errors="coerce")
print(d)
print(d.dt.year.tolist())
print(pd.to_datetime("15/03/2024", format="%d/%m/%Y"))

# Output:
# 0   2024-03-15
# 1   2024-12-01
# 2          NaT
# dtype: datetime64[ns]
# [2024.0, 2024.0, nan]
# 2024-03-15 00:00:00
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. dates को text के रूप में छोड़ देना
  2. format के बिना अस्पष्ट day/month order
  3. अमान्य input से आने वाले NaT को नज़रअंदाज़ करना
चैप्टर सारांश
  • to_datetime text को datetimes में parse करता है
  • format layout control करता है
  • errors=coerce NaT देता है
  • .dt date parts उजागर करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.