pandas 类型错误:strptime() 参数 1 必须是 str,而不是浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46209731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:26:43  来源:igfitidea点击:

TypeError: strptime() argument 1 must be str, not float

python-3.xpandasparsing

提问by Zale Goldart

I'm having parsing errors on my code, below is the code and almost understandable dataset

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

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() argument 1 must be str, not float

类型错误:strptime() 参数 1 必须是 str,而不是浮点数

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

回答by Scott Boston

IIUC, I think you are converting strings into datetime dtypes.

IIUC,我认为您正在将字符串转换为 datetime dtypes。

You can use Pandas to_datetime:

您可以使用Pandasto_datetime

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