0
0
PandasHow-ToBeginner · 3 min read

How to Use dt.day_name in pandas to Get Day Names

Use dt.day_name() on a pandas datetime Series to get the day names as strings. It converts each date to its weekday name like 'Monday' or 'Tuesday'. This is useful for analyzing or grouping data by day of the week.
📐

Syntax

The dt.day_name() is a pandas datetime accessor method used on Series with datetime values. It returns the day name as a string for each date.

  • Series.dt: Accessor for datetime properties.
  • day_name(): Method that returns the weekday name.
python
series.dt.day_name()
💻

Example

This example shows how to create a pandas Series with dates and use dt.day_name() to get the weekday names.

python
import pandas as pd

dates = pd.Series(pd.to_datetime(['2024-06-01', '2024-06-02', '2024-06-03']))
day_names = dates.dt.day_name()
print(day_names)
Output
0 Saturday 1 Sunday 2 Monday dtype: object
⚠️

Common Pitfalls

Common mistakes include:

  • Trying to use dt.day_name without parentheses, which returns a method instead of the day names.
  • Using dt.day_name() on a Series that is not datetime type, causing an error.
  • Not converting strings to datetime before using dt.day_name().
python
import pandas as pd

# Wrong: missing parentheses
# dates = pd.Series(pd.to_datetime(['2024-06-01']))
# print(dates.dt.day_name)  # This prints a method, not day names

# Correct usage

dates = pd.Series(pd.to_datetime(['2024-06-01']))
print(dates.dt.day_name())
Output
0 Saturday dtype: object
📊

Quick Reference

FunctionDescription
dt.day_name()Returns the weekday name as a string for each datetime value
pd.to_datetime()Converts strings or other formats to pandas datetime type
Series.dtAccessor to datetime properties and methods on Series

Key Takeaways

Use dt.day_name() with parentheses to get weekday names from datetime Series.
Ensure your Series is datetime type before using dt.day_name().
Convert strings to datetime using pd.to_datetime() if needed.
dt.day_name() returns day names like 'Monday', 'Tuesday', etc.
Common error: forgetting parentheses or using on non-datetime data.