• Register
0 votes
5.7k 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.

6 5 3
7,540 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()
9 7 4
38,600 points

Related questions

0 votes
1 answer 44 views
44 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
0 votes
1 answer 5 views
5 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 2.9k points
1 vote
1 answer 3 views
3 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 Shovo210 13.3k points
0 votes
1 answer 7 views
7 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 2.3K views
2.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 20 views
20 views
Problem: HELP! I need help understanding this error: Can not merge dataframe with instance of type <class 'pandas.core.series.series'>.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3K views
3K views
Problem : I have the two DataFrames which I would want to merge. I have referred many documents and also tried to perform many operations but I am not sure what to do now. Please find my two DataFrames as below: DataFrame1: id name type currency 0 BTTA.S Apple ... here I met with the exception as below : ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>
asked Dec 24, 2019 alecxe 7.5k points
0 votes
0 answers 102 views
102 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 100 views
100 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
1 answer 21 views
21 views
Problem: Why is this still happening: Cannot cast array data from dtype('float64') to dtype('<u32') according to the rule 'safe'
asked Mar 13 Wafa Abu Yousef 6.1k points