0

I have a Pandas Dataframe where when doing

mydf.info() 

all the datatypes are non-null object

Then I do:

mydf['CCI'] = mydf['CCI'].astype(str)

Afterwards

mydf.info()

shows exactly the same result as before trying to change the data type. Why is it that this line of code does not change the type of data of the DF?

JFerro
  • 3,203
  • 7
  • 35
  • 88
  • What does your dataframe consist of and what are your expected results? It will definitely help to include an example. – BernardL Jan 27 '20 at 08:32
  • it should work. ran on a sample test on my end and dtype changed from float to object. – sammywemmy Jan 27 '20 at 08:32
  • 1
    Strings are seen as `object` when describled by `pandas.info` method. – Cowflu Jan 27 '20 at 08:40
  • ```mydf.info()``` will have label *Data columns* that gives details about the individual columns. For str data type, the column will be showed as type 'object'. If you are seeing the exact same result, it is possible that your column has string data initially too. – Vishakha Lall Jan 27 '20 at 08:50
  • clear and restart the kernel, then try – The Guy Jan 27 '20 at 09:00

1 Answers1

0

This is because there is a difference between string type in numpy and pandas. Numpy has numpy.str however pandas chooses to be closer to python hence the type O.

This answers it very clearly: https://stackoverflow.com/a/34884078/4551984

quest
  • 3,576
  • 2
  • 16
  • 26
  • @cowflu: "Strings are seen as object when describled by pandas.info method. " I did not know that. That means when df.info() you will never see "string" or "str" – JFerro Jan 27 '20 at 09:08
  • Indeed, that's my understanding too. – quest Jan 27 '20 at 10:25