Get weekdays as column based on index

Viewed 32

I tried this code to get the following output:

idx = pd.date_range("2021-06-08", periods=3, freq="D")
ts = pd.Series(['Tuesday', 'Wednesday', 'Thursday'], index=idx)
ts

2021-06-08      Tuesday
2021-06-09    Wednesday
2021-06-10     Thursday
Freq: D, dtype: object

But I don't want to pass list of days. I want that this should extract weekday from index only. I tried following code to get this but this is giving me error:

idx = pd.date_range("2021-06-08", periods=3, freq="D")
ts = pd.Series((x for x in idx.weekday()), index=idx)

Do anyone have any idea of how to get this and suggest what needs to be changed.

1 Answers

Use DatetimeIndex.day_name:

idx = pd.date_range("2021-06-08", periods=3, freq="D")
ts = pd.Series(idx.day_name(), index=idx)
print (ts)
2021-06-08      Tuesday
2021-06-09    Wednesday
2021-06-10     Thursday
Freq: D, dtype: object
Related