如何在python中将Series类型的列转换为datetime weekdays格式?

时间:2016-06-28 02:07:33

标签: python python-2.7 datetime pandas datetime-format

我有以下数据和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消息。我只是  尝试将类型系列的列数据转换为日期工作日  格式

  

datetime.strptime(df ['Session Submitted'],'%Y-%m-%d%H:%M')。strptime('%A')
  TypeError:必须是字符串,而不是Series

1 个答案:

答案 0 :(得分:1)

将参数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