我有以下数据和python代码
Time Started Date Submitted Status
10/29/2015 17:34 10/29/2015 17:34 Complete
10/29/2015 17:35 10/29/2015 17:35 Complete
10/29/2015 17:36 10/29/2015 17:37 Complete
import pandas as pd
from datetime import datetime, timedelta
from pandas import Series, DataFrame
df = pd.read_csv('sample.csv')
datetime.strptime(df['Date Submitted'],'%Y-%m-%d %H:%M').strptime('%A')
当我尝试运行以下代码时,我得到TypeError消息。我只是尝试将类型series的列数据转换为日期时间工作日格式的
datetime。strptime (df("会话提交")、"Y % - % - % d % H: % m ') .strptime (' % ')
TypeError:必须是字符串,而不是Series
将参数parse_dates
添加到read_csv
中以转换为datetime
:
import pandas as pd
import io
temp=u"""Time Started,Date Submitted,Status
10/29/2015 17:34,10/29/2015 17:34,Complete
10/29/2015 17:35,10/29/2015 17:35,Complete
10/29/2015 17:36,10/29/2015 17:37,Complete"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), parse_dates=[0,1])
print (df)
Time Started Date Submitted Status
0 2015-10-29 17:34:00 2015-10-29 17:34:00 Complete
1 2015-10-29 17:35:00 2015-10-29 17:35:00 Complete
2 2015-10-29 17:36:00 2015-10-29 17:37:00 Complete
print (df.dtypes)
Time Started datetime64[ns]
Date Submitted datetime64[ns]
Status object
dtype: object
然后使用dt.strftime
:
df['Date Submitted'] = df['Date Submitted'].dt.strftime('%A')
print (df)
Time Started Date Submitted Status
0 2015-10-29 17:34:00 Thursday Complete
1 2015-10-29 17:35:00 Thursday Complete
2 2015-10-29 17:36:00 Thursday Complete
另一个解决方案是dt.weekday_name
(0.18.1新版本):
df['Date Submitted'] = df['Date Submitted'].dt.weekday_name
print (df)
Time Started Date Submitted Status
0 2015-10-29 17:34:00 Thursday Complete
1 2015-10-29 17:35:00 Thursday Complete
2 2015-10-29 17:36:00 Thursday Complete