• Register
0 votes
8.2k views

Problem :

I have the pandas data frame with some of the categorical predictors or variables as 0 & 1, and some of the numeric variables. When I fit that to a stasmodel like below :

est = sm.OLS(y, X).fit()

It throws the below error :

Pandas data cast to numpy dtype of object. Check input data with np.asarray(data). 

I tried to convert all of the the dtypes of the DataFrame using below code:

 df.convert_objects(convert_numeric=True)

After this all the dtypes of dataframe variables appeaerd as int32 or int64. But at the end of it, it still shows the dtype: object, like below :

5516        int32
5523        int32
5525        int32
5531        int32
5533        int32
5542        int32
5562        int32
sex         int64
race        int64
dispstd     int64
age_days    int64
dtype: object

Here 5516, 5523 are variable labels.

Any clue? I just need to build the multi-regression model on more than the hundreds of variables. For that I have concatenated the 3 pandas DataFrames to come up with the final DataFrame to be used in the model building.

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

If X is your dataframe, then try to use the .astype method to convert to the float when running your model as shown below:

est = sm.OLS(y, X.astype(float)).fit()

 OR

If both the y(dependent) and X are taken from the data frame then type cast both as shown below :-

est = sm.OLS(y.astype(float), X.astype(float)).fit()
38.6k points

Related questions

0 votes
1 answer 222 views
222 views
Problem : Any clue: Pandas data cast to numpy dtype of object. check input data with np.asarray(data)..
asked Mar 6 Wafa Abu Yousef 6.1k points
1 vote
1 answer 31 views
31 views
Problem: I need solution of this error; Valueerror: numpy.dtype has the wrong size, try recompiling.
asked Apr 26 PkGuy 25.9k points
0 votes
1 answer 93 views
93 views
Problem: In my computer this error is showing by a long time, i wana to fix this problem, can anyone help me on this?
asked Apr 3 niyatiry 3.9k points
1 vote
1 answer 65 views
65 views
Problem : I try to create the top 5 rows of my price column. But I got this error : AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas Any ideas on what I need to do to fix this? Thanks!
asked Mar 31 Sabbir Hossain 14.6k points
0 votes
1 answer 40 views
40 views
Problem: I am getting bellow error: attributeerror: can only use .str accessor with string values, which use np.object_ dtype in pandas
asked Mar 13 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3k views
3k views
Problem : I am getting bellow error attributeerror: can only use .str accessor with string values, which use np.object_ dtype in pandas
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
1 answer 236 views
236 views
Could anyone provide a sample of the column data that you're trying to replace? That would help a lot.
asked Dec 14, 2020 TeamScript 13.5k points
0 votes
0 answers 276 views
276 views
when I tried to use str.replace it gave this message dc_listings['price'].str.replace(',', '') AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas Here are the top 5 rows of my price column. This stack overflow thread ... error-can-only-use-str-accessor-with-string-values to check if my column has NAN values but non of the values in my column are NAN
asked Oct 27, 2020 psandprop 2.4k points
0 votes
1 answer 14 views
14 views
Problem: Numpy.ndarray' object has no attribute 'head'?
asked Jul 7 Nahil 128k points
0 votes
1 answer 4 views
4 views
Problem: Support is needed because I have been unable to find a solution : Specify dtype option on import or set low_memory=false
asked Jun 25 nila 121k points