DatetimeIndex की जानकारी
DatetimeIndex dates को row labels बना देता है, जिससे date strings से slicing और time-based tools अनलॉक हो जाते हैं।
In this page:
Syntax
df["date"] = pd.to_datetime(df["date"])
df = df.set_index("date")
df.loc["2024-03"]
DatetimeIndex
convert करने के बाद किसी datetime column को set_index से index बनाएं। फिर आप df["2024-01"] या df["2024-01-05":"2024-01-10"] जैसी strings से slice कर सकते हैं। .month और .dayofweek जैसे index properties सीधे उपलब्ध हैं।
Note:
df.loc["2024-02"] जैसा partial string indexing पूरा महीना select करता है।
उदाहरण: DatetimeIndex
import pandas as pd
df = pd.DataFrame({"date": pd.to_datetime(["2024-01-05", "2024-01-20", "2024-02-03"]), "sales": [10, 20, 30]}).set_index("date")
print(df.loc["2024-01"])
print(df.index.month.tolist())
# Output:
# sales
# date
# 2024-01-05 10
# 2024-01-20 20
# [1, 1, 2]
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. #}
आम गलतियां
- पहले datetime में convert करना भूल जाना
- Unsorted index पर slicing
- text column को index के रूप में उपयोग करना
चैप्टर सारांश
- पहले convert करें फिर set_index
- date strings से slice करें
- Partial strings periods select करते हैं
- Index month और dayofweek उजागर करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: