Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在使用astype获取ValueError时从数据帧中删除错误行_Python_Pandas - Fatal编程技术网

Python 如何在使用astype获取ValueError时从数据帧中删除错误行

Python 如何在使用astype获取ValueError时从数据帧中删除错误行,python,pandas,Python,Pandas,我使用pandas的astype函数将字符串解析为datetime64[ns]格式的数据,但由于原始数据中存在一些异常值,因此导致程序出错 我想从ValueError异常中获取错误的数据索引并删除索引数据,而不是因为ValueError而中断程序。或者有其他方法来实现我的目标吗 在按astype解析datetime时,我收到了一个错误提示,提示如下。我想从ValueError异常中获取错误的数据索引,并删除索引数据: File "/home/xiaopeng/anaconda3/envs/t

我使用pandas的astype函数将字符串解析为datetime64[ns]格式的数据,但由于原始数据中存在一些异常值,因此导致程序出错

我想从ValueError异常中获取错误的数据索引并删除索引数据,而不是因为ValueError而中断程序。或者有其他方法来实现我的目标吗

在按astype解析datetime时,我收到了一个错误提示,提示如下。我想从ValueError异常中获取错误的数据索引,并删除索引数据:

  File "/home/xiaopeng/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/dtypes/cast.py", line 636, in astype_nansafe
    return arr.astype(dtype)
ValueError: Error parsing datetime string "2017-06-01VERSION=1.0" at position 10
代码如下,此函数的主要功能是从文本文件中读取数据,并解析数据:

def file_to_df(file):
    print('converting file:%r(%r MB)' %(file,(os.path.getsize(file)/(1024*1024))))

    df = pd.read_csv(file, sep='\t', header=None, names=columns)

    for k in df.columns:
        _, df[k] = df[k].astype(str).str.split('=',1).str

    df = df[columns_use]

    # startswith() ,delete the wrong data when startswith is not '20'
    df = df[df['PASSTIME'].astype(str).str.startswith("20")]

    print('Log: Get %r number of data' % len(df))

    df['PASSTIME'] = df['PASSTIME'].astype(str).str.replace(' ', '?', n=1)
    df['PASSTIME'] = df['PASSTIME'].astype(str).str.replace(' ', '.', n=1)
    df['PASSTIME'] = df['PASSTIME'].astype(str).str.replace('?', ' ', n=1)

    df['PASSTIME'] = df['PASSTIME'].astype('datetime64[ns]')

    return df
解析错误数据如下:

VERSION=1.0 PASSTIME=2017-06-01 11:01:46 625    CARSTATE=1  ...
VERSION=1.0 PASSTIME=2017-06-01VERSION=1.0  PASSTIME=2017-06-01 11:04:02 618    CARSTATE=1  ...
VERSION=1.0 PASSTIME=2017-06-01 11:04:49 595    CARSTATE=1  ...
我认为您需要+来删除NaT行:


你能添加一些数据样本吗?但主要的问题是您需要df['PASSTIME']=pd.to_datetimedf['PASSTIME'],errors='concurve'将日期转换为日期时间。如果某些数据不正确,例如2017-06-01版本=1.0,则函数返回NaT。所以首先需要干净的数据,然后解析它。使用to_datetime函数可以很好地工作。非常感谢。修改后的代码是:{df['PASSTIME']=pd.to_datetimedf['PASSTIME'],errors='concure'df=df[~df['PASSTIME']].isin[pd.NaT]}我是否不再需要使用df['PASSTIME']=df['PASSTIME']。astype'datetime64[ns]',因为to_datetime函数已将PASSTIME列类型更改为datetime64[ns]?否,df['PASSTIME']=df['PASSTIME'].astype'datetime64[ns]'不是必需的,因为astype函数无法将数据转换为datetimes。我使用df=df[~df['PASSTIME']].isin[pd.NaT]],我想问一下df.dropna的性能是否更好?这是一个非常有趣的问题,请参阅获取计时信息。所以,如果需要更快的解决方案,需要df=df[df['PASSTIME'].notnull],但它只快了一点。非常感谢。这是我在stackoverflow中的第一个问题。因为你的回答,我更喜欢这个网站
df['PASSTIME'] = pd.to_datetime(df['PASSTIME'], errors='coerce')
df = df.dropna('PASSTIME')