0

I have a dataframe and when I do df.dtypes in shows the column is an object

I do df["column_name"] = df["column_name"].astype(str)

and again check df.dtypes it still shows the column type as object. Any idea bow to fix this.

When I do df['column_name'].to_string().dtype

I get an error AttributeError: 'unicode' object has no attribute 'dtype'

6
  • Try df["column_name"] = df["column_name"].astype("string") Commented Aug 7, 2020 at 20:51
  • @bigbounty I tried using this too, it doesn't work Commented Aug 7, 2020 at 20:59
  • Refer pandas.pydata.org/pandas-docs/stable/user_guide/text.html for different ways to converting to string Commented Aug 7, 2020 at 21:00
  • if your try print(df['column_name'].dtypes) - what do you get? Commented Aug 7, 2020 at 21:18
  • It says object @ChristianMagelssen Commented Aug 7, 2020 at 21:19

2 Answers 2

1

In Pandas, there are only these datatypes: boolean, integer, float, datetime and object. Objects are almost always strings but can be any Python object. This is the reason why df.dtypes shows an object.

Sign up to request clarification or add additional context in comments.

4 Comments

I want to convert it to string as I'm unable to do a join on the column. But the conversion doesn't work.
it's just a regular dataframe and converting the object to string isn't working how it usually works for some reason.
I have had similar issues. One time I had to convert it to .astype(int) before .astype(str). Other types it has been due to missing values
I updated the question with some new info not sure if that helps.
0

Series can be converted to the string datatype, give this a try:

df['column_name'] = pd.Series(df['column_name'], dtype="string")

Or give this a try

df['column_name']=df.column_name.convert_dtypes()

1 Comment

@JohnConstantine See my answer

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.