Unable to Parse pandas Series to datetime

Viewed 1410

I'm importing a csv files which contain a datetime column, after importing the csv, my data frame will contain the Dat column which type is pandas.Series, I need to have another column that will contain the weekday:

import pandas as pd
from datetime import datetime

data = 
pd.read_csv("C:/Users/HP/Desktop/Fichiers/Proj/CONSOMMATION_1h.csv")
print(data.head())

all the data are okay, but when I do the following:

data['WDay'] = pd.to_datetime(data['Date'])
print(type(data['WDay']))
# the output is 
<class 'pandas.core.series.Series'>

the data is not converted to datetime, so I can't get the weekday.

2 Answers

Problem is you need dt.weekday with .dt:

data['WDay'] = data['WDay'].dt.weekday

Without dt is used for DataetimeIndex (not in your case) - DatetimeIndex.weekday:

data['WDay'] = data.index.weekday

use the command data.dtypes to check the type of the columns.

Related