2

When importing my .txt file, I am getting the following error:

Can't convert 'int' object to str implicitly

I import my .txt file as follows:

activityHeaders = ['Type', 'AccountID', 'ConID', 'SecurityID', 'Symbol', 'BBTicker', 'Currency', 'BaseCurrency', 'TradeDate', 'SettleDate', 'TransactionType', 'Quantity', 'UnitPrice', 'GrossAmount', 'SECFee', 'Commission', 'NetInBase', 'FXRatetoBase', 'Other1', 'Other2', 'Description']

dfActivity = pd.read_csv(activityFileUrl, skiprows=[1], header=activityHeaders, error_bad_lines=False)

and my .txt file looks as follows:

"H","I000000","Activity","20100407","16:02:38","20100329","1.0"
"D","I000000","","","","","CAD","EUR","20100329","20100329","WITH","0","0","-14.88","0","0","0","-14.88","-10.8158","",,"CASH TRANSFER (INTERNAL)"
"D","I000000","","","","","AUD","EUR","20100328","20100328","ADJ","0","0","4","0","0","0","4","2.7211","",,"CLIENT FEE (U000001, Commission)"
"D","U000001","37036548","DE000A0F6MD5","PRA","STK","EUR","EUR","20100329","20100331","SELL","-300","7.776","-2332.8","0","-6","0","2326.8","2326.8","405346125","FI","TRADE PRAKTIKER BAU-UND HEIMWERK A"

I don't understand where the int could come from. Note that I skip the first and last row using the skiprows and error_bad_lines. I put header also to None and it returned the same error.

3
  • What does activityHeaders look like? Commented Mar 30, 2017 at 10:58
  • Added it to the description Commented Mar 30, 2017 at 11:00
  • That worked indeed. But now I need to add these columns and it returns that the first line has not been skipped. I.e. it thinks that there are 7 columns and not 21? Commented Mar 30, 2017 at 11:03

2 Answers 2

3

You need change header to names if need column names by list activityHeaders.

df = pd.read_csv(StringIO(temp), names=activityHeaders, skiprows=1, error_bad_lines=False)
print (df)
      Type   AccountID         ConID SecurityID Symbol BBTicker Currency  \
D  I000000         NaN           NaN        NaN    NaN      CAD      EUR   
D  I000000         NaN           NaN        NaN    NaN      AUD      EUR   
D  U000001  37036548.0  DE000A0F6MD5        PRA    STK      EUR      EUR   

   BaseCurrency  TradeDate SettleDate                 ...                  \
D      20100329   20100329       WITH                 ...                   
D      20100328   20100328        ADJ                 ...                   
D      20100329   20100331       SELL                 ...                   

   Quantity  UnitPrice  GrossAmount  SECFee  Commission  NetInBase  \
D     0.000     -14.88            0       0           0     -14.88   
D     0.000       4.00            0       0           0       4.00   
D     7.776   -2332.80            0      -6           0    2326.80   

   FXRatetoBase       Other1  Other2                         Description  
D      -10.8158          NaN     NaN            CASH TRANSFER (INTERNAL)  
D        2.7211          NaN     NaN    CLIENT FEE (U000001, Commission)  
D     2326.8000  405346125.0      FI  TRADE PRAKTIKER BAU-UND HEIMWERK A  

[3 rows x 21 columns]

Also if need NOT skip second row omit skiprows:

df = pd.read_csv(StringIO(temp), names=activityHeaders, error_bad_lines=False)
print (df)
  Type AccountID     ConID    SecurityID    Symbol  BBTicker Currency  \
0    H   I000000  Activity      20100407  16:02:38  20100329      1.0   
1    D   I000000       NaN           NaN       NaN       NaN      CAD   
2    D   I000000       NaN           NaN       NaN       NaN      AUD   
3    D   U000001  37036548  DE000A0F6MD5       PRA       STK      EUR   

  BaseCurrency   TradeDate  SettleDate     ...      Quantity  UnitPrice  \
0          NaN         NaN         NaN     ...           NaN        NaN   
1          EUR  20100329.0  20100329.0     ...           0.0      0.000   
2          EUR  20100328.0  20100328.0     ...           0.0      0.000   
3          EUR  20100329.0  20100331.0     ...        -300.0      7.776   

   GrossAmount  SECFee  Commission  NetInBase  FXRatetoBase     Other1  \
0          NaN     NaN         NaN        NaN           NaN        NaN   
1       -14.88     0.0         0.0        0.0        -14.88   -10.8158   
2         4.00     0.0         0.0        0.0          4.00     2.7211   
3     -2332.80     0.0        -6.0        0.0       2326.80  2326.8000   

        Other2  Description  
0          NaN          NaN  
1          NaN          NaN  
2          NaN          NaN  
3  405346125.0           FI  

[4 rows x 21 columns]
Sign up to request clarification or add additional context in comments.

Comments

1

Drop the headers parameter altogether. headers is used as an integer list to tell pandas which row(s) to use as your header.

Change to skiprows=[0] and you should be good.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.