'Difference between str() and astype(str)?

I want to save the dataframe df to the .h5 file MainDataFile.h5 :

df.to_hdf ("c:/Temp/MainDataFile.h5", "MainData", mode = "w", format = "table", data_columns=['_FirstDayOfPeriod','Category','ChannelId'])

and get the following error :

*** Exception: cannot find the correct atom type -> > [dtype->object,items->Index(['Libellé_Article', 'Libellé_segment'], dtype='object')]

If I modifify the column 'Libellé_Article' in this way :

df['Libellé_Article'] = str(df['Libellé_Article'])

there is no error anymore, whereas I still get the error message when doing :

df['Libellé_Article'] = df['Libellé_Article'].astype(str)

The problem is that using str() is blowing up my ram.

Any idea ?



Solution 1:[1]

str(df['Libellé_Article']) will convert the contents of the entire column in to single string. It will end up with a very big string. And thats the reason for blowing up your RAM

For example

>> df = pd.DataFrame([1,2,3], columns=['A'])
>> df['A']
0    1
1    2
2    3 
Name: A, dtype: int64

>> str(df['A'])
 '0    1\n1    2\n2    3\nName: A, dtype: int64'
>> df['A'].astype(str)
0    1
1    2
2    3
Name: A, dtype: object

So you should use .astype(str) only, if you want to convert your entire column to type string

Solution 2:[2]

  • The difference here is that .astype(str) is a method for a Pandas series and str() is a function.
  • This is why : .astype(str) will work on series and not on int while str() will work on both.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Elad Rubi