Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get the day from a date in Pandas
Pandas provides several methods to extract the day name from dates. Whether you're working with single dates or multiple date strings, you can use dt.dayofweek, dt.day_name(), or dt.strftime() to get day information in different formats.
Using dt.dayofweek with Manual Mapping
The dt.dayofweek property returns numeric values (0-6) for days, which you can map to day names ?
import pandas as pd
# Create a date range
dates = pd.date_range(start='2022-01-01', end='2022-01-07')
# Create a DataFrame with the dates
df = pd.DataFrame({'date': dates})
# Add a column with the day of the week as numbers
df['day_number'] = df['date'].dt.dayofweek
# Map the day number to its name
day_mapping = {
0: 'Monday',
1: 'Tuesday',
2: 'Wednesday',
3: 'Thursday',
4: 'Friday',
5: 'Saturday',
6: 'Sunday'
}
df['day_name'] = df['day_number'].map(day_mapping)
print(df)
date day_number day_name
0 2022-01-01 5 Saturday
1 2022-01-02 6 Sunday
2 2022-01-03 0 Monday
3 2022-01-04 1 Tuesday
4 2022-01-05 2 Wednesday
5 2022-01-06 3 Thursday
6 2022-01-07 4 Friday
Using pd.to_datetime() and dt.day_name()
Convert date strings to datetime objects and use dt.day_name() for direct day name extraction ?
import pandas as pd
# Create DataFrame with date string
df = pd.DataFrame({'date': ['2023-04-10']})
# Convert to datetime and extract day name
df['date'] = pd.to_datetime(df['date'])
df['day'] = df['date'].dt.day_name()
print(df)
date day
0 2023-04-10 Monday
Working with Multiple Dates
Process multiple date strings at once using vectorized operations ?
import pandas as pd
# Create DataFrame with multiple date strings
dates_df = pd.DataFrame({
'date': ['2019-03-07', '2020-03-07', '2021-03-07', '2022-03-07', '2023-03-07']
})
# Convert to datetime and extract day names
dates_df['date'] = pd.to_datetime(dates_df['date'])
dates_df['day'] = dates_df['date'].dt.day_name()
print(dates_df)
date day
0 2019-03-07 Thursday
1 2020-03-07 Saturday
2 2021-03-07 Sunday
3 2022-03-07 Monday
4 2023-03-07 Tuesday
Using dt.strftime() for Abbreviated Names
Use strftime() to get abbreviated day names or custom date formats ?
import pandas as pd
# Create DataFrame with date string
df = pd.DataFrame({'date': ['2023-04-10']})
# Convert to datetime
df['date'] = pd.to_datetime(df['date'])
# Extract day information in different formats
df['day_number'] = df['date'].dt.dayofweek
df['day_abbreviated'] = df['date'].dt.strftime('%a')
df['day_full'] = df['date'].dt.day_name()
print(df)
date day_number day_abbreviated day_full
0 2023-04-10 0 Mon Monday
Comparison of Methods
| Method | Output Format | Best For |
|---|---|---|
dt.dayofweek |
Numbers (0-6) | Numerical analysis or custom mapping |
dt.day_name() |
Full day names | Readable output and reports |
dt.strftime('%a') |
Abbreviated names | Space-efficient display |
Conclusion
Use dt.day_name() for readable full day names, dt.dayofweek for numerical operations, and dt.strftime() for custom formatting. All methods work efficiently with both single dates and multiple date columns.
