TypeError:strptime()参数1必须是str,而不是float

时间:2017-09-14 02:50:36

标签: python-3.x pandas parsing

我的代码上有解析错误,下面是代码和几乎可以理解的数据集

import numpy as np
import pandas as pd
from datetime import datetime as dt

data0 = pd.read_csv('2009-10.csv')
data1 = pd.read_csv('2010-11.csv')

def parse_date(date):
    if date == '':
        return None
    else:
        return dt.strptime(date, '%d/%m/%y').date()

data0.Date = data0.Date.apply(parse_date)
data1.Date = data1.Date.apply(parse_date)

TypeError:strptime()参数1必须是str,而不是float

Date    HomeTeam    AwayTeam    FTHG    FTAG    FTR HTHG    HTAG    HTR Referee HS  AS  HST AST HF  AF  HC  AC  HY  AY  HR  AR  B365H   B365D   B365A
15/08/09    Aston Villa Wigan   0   2   A   0   1   A   M Clattenburg   11  14  5   7   15  14  4   6   2   2   0   0   1.67    3.6 5.5
15/08/09    Blackburn   Man City    0   2   A   0   1   A   M Dean  17  8   9   5   12  9   5   4   2   1   0   0   3.6 3.25    2.1
15/08/09    Bolton  Sunderland  0   1   A   0   1   A   A Marriner  11  20  3   13  16  10  4   7   2   1   0   0   2.25    3.25    3.25
15/08/09    Chelsea Hull    2   1   H   1   1   D   A Wiley 26  7   12  3   13  15  12  4   1   2   0   0   1.17    6.5 21
15/08/09    Everton Arsenal 1   6   A   0   3   A   M Halsey    8   15  5   9   11  13  4   9   0   0   0   0   3.2 3.25    2.3

Date    HomeTeam    AwayTeam    FTHG    FTAG    FTR HTHG    HTAG    HTR Referee HS  AS  HST AST HF  AF  HC  AC  HY  AY  HR  AR  B365H   B365D   B365A
14/08/10    Aston Villa West Ham    3   0   H   2   0   H   M Dean  23  12  11  2   15  15  16  7   1   2   0   0   2   3.3 4
14/08/10    Blackburn   Everton 1   0   H   1   0   H   P Dowd  7   17  2   12  19  14  1   3   2   1   0   0   2.88    3.25    2.5
14/08/10    Bolton  Fulham  0   0   D   0   0   D   S Attwell   13  12  9   7   12  13  4   8   1   3   0   0   2.2 3.3 3.4
14/08/10    Chelsea West Brom   6   0   H   2   0   H   M Clattenburg   18  10  13  4   10  10  3   1   1   0   0   0   1.17    7   17
14/08/10    Sunderland  Birmingham  2   2   D   1   0   H   A Taylor    6   13  2   7   13  10  3   6   3   3   1   0   2.1 3.3 3.6
14/08/10    Tottenham   Man City    0   0   D   0   0   D   A Marriner  22  11  18  7   13  16  10  3   0   2   0   0   2.4 3.3 3

1 个答案:

答案 0 :(得分:0)

IIUC,我认为您正在将字符串转换为日期时间dtypes。

您可以使用Pandas to_datetime

data0['Date'] = pd.to_datetime(data0['Date'], format='%d/%m/%y')
data1['Date'] = pd.to_datetime(data1['Date'], format='%d/%m/%y')