错误值错误:日期超出了月份的范围

2024-10-02 10:28:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在看一张学生信息样本表,想看看学生最喜欢在哪几天注册课程。脚本在我运行它的第一天就运行良好,我离开了它。几天后,我回来再次查看它,但开始收到ValueError消息

Why did it stop working? No new information was added to the dataset since it worked the first time. The code now fails at df["EnrolmentDate"] = pd.to_datetime(df.EnrolmentDate)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import seaborn as sns
colors = sns.cubehelix_palette(28, rot=-0.4)

df = pd.read_csv("data.csv")

#print(df.dtypes)

# To change the format of the data type from object to datetime.  Has to be run at start of script or the format returns to object.
#df['Enrolment Date'] = pd.to_datetime(df['Enrolment Date'])

print(df.EnrolmentDate.str.slice(0, 10))

df["EnrolmentDate"] = pd.to_datetime(df.EnrolmentDate)
print(df.head())
print(df.dtypes)
#Tells us what day of the week the enrolment date was.  Can also use .dayofyear. Google Pandas API Reference, search for .dt., datetime properties
print(df.EnrolmentDate.dt.weekday_name)

#Shows the latest or greatest enrolment date
print(df.EnrolmentDate.max())

print(df.EnrolmentDate.min())

print(df.EnrolmentDate.max()-df.EnrolmentDate.min())

df["EnrolmentDay"] =  df.EnrolmentDate.dt.weekday_name
print(df.head())

print(df.EnrolmentDay.value_counts())
print(df.EnrolmentDay.value_counts().plot())

#print(df.Day.value_counts().sort_index())

#df.EnrolmentDay.value_counts().sort_index().plot()
# naming the x axis 
plt.xlabel('Day') 
# naming the y axis 
plt.ylabel('No. of Enrolments') 

plt.show()

Tags: ofthetoimportdfdatetimevalueas

热门问题