【问题标题】:How to check for wrong datetime entries (python/pandas)?如何检查错误的日期时间条目(python/pandas)?
【发布时间】:2020-04-01 20:13:06
【问题描述】:

我有一个 Excel 数据集,其中包含员工输入的工作时间的日期时间值。现在年底快到了,他们想报告它,但是它充满了错误的条目。所以我需要清理它。

以下是一些错误条目的示例。

面对此类数据集时,您会采取什么方法?

我首先使用df['Shiftdatum'] = pd.to_datetime(df.Shiftdatum, format='%Y-%m-%d', errors='coerce')将日期列转换为日期时间

在下面的示例数据中,它显示了一个 NaT

如何过滤掉包括行索引在内的这些 NaT?

[Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 Timestamp('2019-03-11 00:00:00'),
 NaT,
 Timestamp('2019-03-12 00:00:00')

初始样本数据:

{0: '2019-03-11 00:00:00',
 1: '2019-03-11 00:00:00',
 2: '2019-03-11 00:00:00',
 3: '2019-03-11 00:00:00',
 4: '2019-03-11 00:00:00',
 5: '2019-03-11 00:00:00',
 6: '2019-03-11 00:00:00',
 7: '2019-03-11 00:00:00',
 8: '2019-03-11 00:00:00',
 9: '2019-03-11 00:00:00',
 10: '2019-03-11 00:00:00',
 11: '2019-03-11 00:00:00',
 12: '2019-03-11 00:00:00',
 13: '2019-03-11 00:00:00',
 14: '2019-03-11 00:00:00',
 15: '2019-03-11 00:00:00',
 16: '33/11/2019',
 17: '2019-03-12 00:00:00',
 18: '2019-03-12 00:00:00',
 19: '2019-03-12 00:00:00'}

【问题讨论】:

标签: python pandas datetime


【解决方案1】:

IIUC,

您可以通过多种方式处理此问题,您可以使用 pd.to_datetime(column,errors='coerce') 并将您的数据分配到新列

然后使用新列,您可以按NaT 过滤并获得唯一的异常值,

让我们说这是结果:

data = ['033-10-2019', '100-03-2019','1003-03-2019','03-10-2019']

df = pd.DataFrame({'date_time' : data})
df['correct'] = pd.to_datetime(df['date_time'],errors='coerce')
print(df)
       date_time    correct
0   033-10-2019        NaT
1   100-03-2019        NaT
2  1003-03-2019        NaT
3    03-10-2019 2019-03-10

现在 - 我们需要在 date_time col 中获取唯一的 NaT 值

errors = df.loc[df['correct'].isnull()]['date_time'].unique().tolist()
out : ['033-10-2019', '100-03-2019', '1003-03-2019']

这有点无聊,您需要检查并修复错误并将正确的值传递到字典中:

correct_dict = {'033-10-2019' : '03-10-2019', '100-03-2019' : '03-10-2019', '1003-03-2019' : '10-03-2019'}

然后将值映射回您的数据框:

df['correct'] = df['correct'].fillna(pd.to_datetime(df['date_time'].map(correct_dict)))
print(df)
      date_time    correct
0   033-10-2019 2019-03-10
1   100-03-2019 2019-03-10
2  1003-03-2019 2019-10-03
3    03-10-2019 2019-03-10

如果您只想删除 NaT 值,您可以在设置列子集的同时 dropna

df = df.dropna(subset=['correct'])

【讨论】:

  • 这是在 35,000 行(和 280,000 个日期时间字段)中找出“超出纳秒时间戳”错误的救星。最终只有 8 个字段的日期值为负。
  • @Adam 我很高兴这个解决方案有帮助,恭喜获得 1k!
【解决方案2】:

如何过滤掉包括行索引在内的这些 NaT?

如果需要找出无效的日期条目,您可以在pd.to_datetime() 之后尝试series.isna()series where()

df=pd.DataFrame.from_dict(d,orient='index',columns=['Shiftdatum'])
#d is the dictionary in the question

s=pd.to_datetime(df.Shiftdatum, format='%Y-%m-%d', errors='coerce').isna()
e=df.Shiftdatum.where(s).dropna()

16    33/11/2019

【讨论】:

    猜你喜欢
    • 2018-10-27
    • 2018-11-21
    • 1970-01-01
    • 2020-06-03
    • 2020-10-06
    • 2023-04-01
    • 2020-03-08
    • 1970-01-01
    • 2018-05-16
    相关资源
    最近更新 更多