Python使用pandas和str.strip崩溃

时间:2014-01-22 15:06:39

标签: python pandas strip

这个最小的代码崩溃了我的Python。 (设置:pandas 0.13.0,python 2.7.3 AMD64,Win7。)

import pandas as pd
input_file = r"c3.csv"
input_df = pd.read_csv(input_file)
for col in input_df.columns:  # strip whitespaces from string values
    if input_df[col].dtype == object:
        input_df[col] = input_df[col].apply(lambda x: x.strip())
print 'start'
for idx in range(len(input_df)):
    input_df['LL'].iloc[idx] = 3
    print idx
print 'finished'

输出:

start
0

Process finished with exit code -1073741819

什么可以防止崩溃:

  1. 从c3.csv中删除行
  2. 从代码中删除.strip()
  3. 更改c3.csv会以意想不到的方式更改for次迭代的数量,直至崩溃。
  4. c3.csv的内容:

     Size    , B/S , Symbol    , Type , BN , Duration , VR , Time    , SR ,LL,
    0, xxxx , xxxx0 ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    00, xxxx , xxxxx ,   ,, xxx , 00000 , 00:00:00 , 000000000 , 00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    0, xxxx , xxxxx ,   ,, xxx , 00000 , 00-00:00:00 , 000000000 , 00-00:00:00 ,
    

1 个答案:

答案 0 :(得分:11)

您正在进行链式分配,其行为可能会以意想不到的方式发生。见这里:http://pandas.pydata.org/pandas-docs/dev/indexing.html#indexing-view-versus-copy。这是在master中修复的,将在0.13.1(即将推出)中工作。见这里:https://github.com/pydata/pandas/pull/6031

这样做不正确:

input_df['LL'].iloc[idx] = 3

取而代之的是:

input_df.ix[ix,'LL'] = 3

甚至更好(因为您将所有行分配给3)

input_df['LL'] = 3

如果你只分配一些行(并且说一个整数/布尔索引器)

input_df.ix[indexer,'LL'] = 3

你也应该这样做来剥离空白:

input_df[col] = input_df[col].str.strip()