Q:

TypeError in pandas Dateframe with 'datetime.date'

I'm new to pandas and I would like to create a DataFrame for each weekday based on a bigger DataFrame with all kind of dates.
I read my initial data from a csv with the method data = pd.read_csv() and then my "Timestamp" column is set to datetime this way : data["Timestamp"] = pd.to_datetime(data["Timestamp"]).
code :
import pandas as pd
from datetime import datetime
import calendar

data = pd.read_csv("stat.csv")
data["Timestamp"] = pd.to_datetime(data["Timestamp"])

dataMonday = data.loc[calendar.day_name[datetime.weekday(data["Timestamp"])] == "Monday"]

Now, here is the output :
TypeError: descriptor 'weekday' for 'datetime.date' objects doesn't apply to a 'Series' object
The only way I've found so far is to iterate with a for loop in the Timestamp column, but it appears to be a bad solution since I can hardly create another Dataframe based on that.

A:

Here is a way to add a column with the name of the weekday.  This approach uses the .dt date accessor, and operates on the series,  which is fast.
import pandas as pd
n = 8
t = pd.DataFrame({'x': [*range(n)], 
                  'Timestamp': pd.date_range(start='2020-01-01', periods=n, freq='D')})
t['Timestamp'] = pd.to_datetime(t['Timestamp']) # not needed in this example
t['weekday'] = t['Timestamp'].dt.day_name()
print(t)

   x  Timestamp    weekday
0  0 2020-01-01  Wednesday
1  1 2020-01-02   Thursday
2  2 2020-01-03     Friday
3  3 2020-01-04   Saturday
4  4 2020-01-05     Sunday
5  5 2020-01-06     Monday
6  6 2020-01-07    Tuesday
7  7 2020-01-08  Wednesday

