1

This question seems very basic and the topic was clearly answered in this question : Change data type of columns in Pandas

I have a DataFrame df. One column is named adress and contains strings :

type(df.loc[0, 'adress'])
>>> str

#Let's check the whole column's type

df.loc[:, 'adress'].dtype
>>> dtype('O')

The dtype is an object, I would like to change it to a string. I tried :

df.loc[:, 'adress'] = df.loc[:, 'adress'].astype(str)
df.loc[:, 'adress'].dtype
>>> dtype('O')

#and

df.loc[:, 'adress'] = df.loc[:, 'adress'].astype('str')
df.loc[:, 'adress'].dtype
>>> dtype('O')

#and

df = df.infer_objects()
df.loc[:, 'adress'].dtype
>>> dtype('O')

I have no idea what I am missing.

I aslo checked that the whole column contains only strings :

for i in df.index:
    if type(df.loc[i, 'adress']) != str:
        print(i)
        wait = input('Up')
print('Done')
>>>Done
Basile
  • 575
  • 1
  • 6
  • 13
  • 1
    if you use pandas>=1.0, then you can try `df['adress'].astype('string')`, see [this](https://pandas.pydata.org/pandas-docs/stable/user_guide/text.html#text-types) and you will get [`StringDtype`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.StringDtype.html#pandas.StringDtype). prior this version then column with string could be only `object` – Ben.T Mar 30 '20 at 18:00

0 Answers0