'AttributeError: 'DataFrame' object has no attribute 'dtype' appeared suddenly
I have df with features in my google colab, and suddenly appeared error:
Code:
df_features['cooling'] = df['cooling'].astype('object')
df_features['view'] = df['view'].astype('object')
cat_features = ['cooling', 'view', 'city_region']
X = df_features.drop('target', axis=1)
y = df_features['target']
num_cols = [col for col in X.columns if X[col].dtype in ['float64','int64']]
cat_cols = [col for col in X.columns if X[col].dtype not in ['float64','int64']]
Here is error: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 5 y = df_features['target'] 6 ----> 7 num_cols = [col for col in X.columns if X[col].dtype in ['float64','int64']] 8 cat_cols = [col for col in X.columns if X[col].dtype not in ['float64','int64']] 9
1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in __getattr__(self, name)
5485 ):
5486 return self[name]
-> 5487 return object.__getattribute__(self, name)
5488
5489 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'dtype'
I already tried to use !pip install --upgrade pandas but it had no success
Solution 1:[1]
You seem to somehow get back a DataFrame and not a Series by calling X[col]. Not sure why, because you did not supply the full structure and data of your dataframe.
.dtype is for pandas Series https://pandas.pydata.org/docs/reference/api/pandas.Series.dtype.html
.dtypes is for pandas Dataframes (and seems also to work with pandas Series) https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dtypes.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SebastianB |
