【问题标题】:How do you search for typing errors (dates) from a dataset and replace them based on criteria?如何从数据集中搜索输入错误(日期)并根据条件替换它们?
【发布时间】:2019-09-30 05:05:05
【问题描述】:

我正在对一个包含多个输入错误的大型旧数据集进行排序。我想通过遍历一行来清理文档,以根据标准查找和更改类型错误。现在我在尝试删除 pandas 中的小时分秒时出现以下错误。

Out of bounds nanosecond timestamp: 3016-03-09 00:00:00

我的想法是这样的:

import pandas as pd

df = pd.read_excel(r'raw data.xlsx', header = 0)


for date in Dates:
    if date is out of bounds date time:
        replace str[0-3] with Year, inplace = True


df['Date'].dt.year,month,day

print(df)

典型的数据集输入错误可能是第 4 项中的日期

Item    Description Date    Year    ...
1   Ketchup400  2015-10-27 00:00:00 2015    ...
2   Ketchup600  2018-02-16 00:00:00 2018    ...
3   Mustard800  2015-10-02 00:00:00 2015    ...
4   Mustard200  3016-03-09 00:00:00 2016    ...
1   Ketchup400  2018-02-26 00:00:00 2018    ...
... ... ... ... ...

我想从日期中删除小时、分钟和秒,更正日期并打印新文件...输出应该类似于:

Item    Description Date    Year    ...
1   Ketchup400  2015-10-27  2015    ...
2   Ketchup600  2018-02-16  2018    ...
3   Mustard800  2015-10-02  2015    ...
4   Mustard200  2016-03-09  2016    ...
1   Ketchup400  2018-02-26  2018    ...
... ... ... ... ...

【问题讨论】:

    标签: pandas date dataset missing-data


    【解决方案1】:

    最简单的解决方案是先将Year 列添加到Date 列,然后将第一个- 转换为日期时间:

    s = df['Year'].astype(str) + '-' + df['Date'].astype(str).str.split('-', n=1).str[1]
    df['Date'] = pd.to_datetime(s, errors='coerce')
    print (df)
       Item Description       Date  Year  ...
    0     1  Ketchup400 2015-10-27  2015  ...
    1     2  Ketchup600 2018-02-16  2018  ...
    2     3  Mustard800 2015-10-02  2015  ...
    3     4  Mustard200 2016-03-09  2016  ...
    4     1  Ketchup400 2018-02-26  2018  ...
    

    另一种性能更好的解决方案是通过to_datetimeerrors='coerce' 参数获取所有未解析的日期时间,通过检查缺失值仅解析过滤后的行,并通过Series.combine_first 添加到原始行,就像第一个解决方案一样:

    date = pd.to_datetime(df['Date'], errors='coerce')
    
    df1 = df[date.isna()]
    print (df1)
       Item Description                 Date  Year  ...
    3     4  Mustard200  3016-03-09 00:00:00  2016  ...
    
    s = (pd.to_datetime(df1['Year'].astype(str) + '-' + 
                        df1['Date'].astype(str).str.split('-', n=1).str[1]))
    print (s)
    3   2016-03-09
    dtype: datetime64[ns]
    
    df['Date'] = date.combine_first(s)
    print (df)
       Item Description       Date  Year  ...
    0     1  Ketchup400 2015-10-27  2015  ...
    1     2  Ketchup600 2018-02-16  2018  ...
    2     3  Mustard800 2015-10-02  2015  ...
    3     4  Mustard200 2016-03-09  2016  ...
    4     1  Ketchup400 2018-02-26  2018  ...
    

    【讨论】:

    • 我得到错误:'Can only use .str accessor with string values, which use np.object_ dtype in pandas' 当使用行 s=... 时。日期作为 dtype 对象导入。
    • @aosa - 你能把df['Date'].str.split('-', n=1).str[1]改成df['Date'].astype(str).str.split('-', n=1).str[1]吗?
    【解决方案2】:

    你可以试试这个,也可以得到无效的日期作为 NaT ;)

    df['Date2'] = pd.to_datetime(df['Date'], errors='coerce')
    

    注意:

          Item Description                 Date  Year
    0     1  Ketchup400  2015-10-27 00:00:00  2015
    1     2  Ketchup600  2018-02-16 00:00:00  2018
    2     3  Mustard800  2015-10-02 00:00:00  2015
    3     4  Mustard200  3016-03-09 00:00:00  2016
    4     1  Ketchup400  2018-02-26 00:00:00  2018
       Item Description                 Date  Year      Date2
    0     1  Ketchup400  2015-10-27 00:00:00  2015 2015-10-27
    1     2  Ketchup600  2018-02-16 00:00:00  2018 2018-02-16
    2     3  Mustard800  2015-10-02 00:00:00  2015 2015-10-02
    3     4  Mustard200  3016-03-09 00:00:00  2016        NaT
    4     1  Ketchup400  2018-02-26 00:00:00  2018 2018-02-26
    

    然后尽可能手动修复解析错误,因为 NaT 将帮助您识别问题。

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 2014-12-13
      • 1970-01-01
      • 2020-05-03
      相关资源
      最近更新 更多