What is the meaning of: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data) and how can be solved?

I’m trying to model a time series for a stock price with the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import opendatasets as od
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
from sklearn.inspection import permutation_importance
import statsmodels.api as sm
from statsmodels.tsa.stattools import acf, pacf
import datetime
import warnings
warnings.filterwarnings('ignore')
</code>
<code>import opendatasets as od import numpy as np import pandas as pd import plotly.graph_objects as go from plotly.subplots import make_subplots import tensorflow as tf from sklearn.preprocessing import StandardScaler from sklearn.inspection import permutation_importance import statsmodels.api as sm from statsmodels.tsa.stattools import acf, pacf import datetime import warnings warnings.filterwarnings('ignore') </code>
import opendatasets as od
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import tensorflow as tf

from sklearn.preprocessing import StandardScaler
from sklearn.inspection import permutation_importance
import statsmodels.api as sm
from statsmodels.tsa.stattools import acf, pacf
import datetime

import warnings
warnings.filterwarnings('ignore')

and I got the following structure:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> date close
0 2021-01-04 04:00:00-05:00 133.740000
1 2021-01-04 05:00:00-05:00 133.600000
2 2021-01-04 06:00:00-05:00 134.220000
3 2021-01-04 07:00:00-05:00 133.750000
4 2021-01-04 08:00:00-05:00 134.020000
... ... ...
4070 2021-12-30 13:30:00-05:00 178.975006
4071 2021-12-30 14:30:00-05:00 178.960007
4072 2021-12-30 15:30:00-05:00 178.250000
4073 2021-12-30 16:00:00-05:00 178.190000
4074 2021-12-30 17:00:00-05:00 177.980000
</code>
<code> date close 0 2021-01-04 04:00:00-05:00 133.740000 1 2021-01-04 05:00:00-05:00 133.600000 2 2021-01-04 06:00:00-05:00 134.220000 3 2021-01-04 07:00:00-05:00 133.750000 4 2021-01-04 08:00:00-05:00 134.020000 ... ... ... 4070 2021-12-30 13:30:00-05:00 178.975006 4071 2021-12-30 14:30:00-05:00 178.960007 4072 2021-12-30 15:30:00-05:00 178.250000 4073 2021-12-30 16:00:00-05:00 178.190000 4074 2021-12-30 17:00:00-05:00 177.980000 </code>
                     date         close
0   2021-01-04 04:00:00-05:00   133.740000
1   2021-01-04 05:00:00-05:00   133.600000
2   2021-01-04 06:00:00-05:00   134.220000
3   2021-01-04 07:00:00-05:00   133.750000
4   2021-01-04 08:00:00-05:00   134.020000
... ... ...
4070    2021-12-30 13:30:00-05:00   178.975006
4071    2021-12-30 14:30:00-05:00   178.960007
4072    2021-12-30 15:30:00-05:00   178.250000
4073    2021-12-30 16:00:00-05:00   178.190000
4074    2021-12-30 17:00:00-05:00   177.980000

As a result I have

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>0 int64
dtype: object
</code>
<code>0 int64 dtype: object </code>
0    int64
dtype: object

However when I try to split the time series to train and test for an MLP model and fit it like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># splitting time series to train and test subsets
y_train = df.iloc[:-8766, :].copy()
y_test = df.iloc[-8766:, :].copy()
# Unobserved Components model definition
model = sm.tsa.UnobservedComponents(y_train,
level='dtrend',
irregular=True,
stochastic_level = False,
stochastic_trend = False,
stochastic_freq_seasonal = [False, False, False],
freq_seasonal=[{'period': 24, 'harmonics': 1},
{'period': 168, 'harmonics': 1},
{'period': 8766, 'harmonics': 2}])
# fitting model to train data
model_results = model.fit()
# printing statsmodels summary for model
print(model_results.summary())
</code>
<code># splitting time series to train and test subsets y_train = df.iloc[:-8766, :].copy() y_test = df.iloc[-8766:, :].copy() # Unobserved Components model definition model = sm.tsa.UnobservedComponents(y_train, level='dtrend', irregular=True, stochastic_level = False, stochastic_trend = False, stochastic_freq_seasonal = [False, False, False], freq_seasonal=[{'period': 24, 'harmonics': 1}, {'period': 168, 'harmonics': 1}, {'period': 8766, 'harmonics': 2}]) # fitting model to train data model_results = model.fit() # printing statsmodels summary for model print(model_results.summary()) </code>
# splitting time series to train and test subsets
y_train = df.iloc[:-8766, :].copy()
y_test = df.iloc[-8766:, :].copy()

# Unobserved Components model definition
model = sm.tsa.UnobservedComponents(y_train,
                                    level='dtrend',
                                    irregular=True,
                                    stochastic_level = False,
                                    stochastic_trend = False,
                                    stochastic_freq_seasonal = [False, False, False],
                                    freq_seasonal=[{'period': 24, 'harmonics': 1},
                                                    {'period': 168, 'harmonics': 1},
                                                    {'period': 8766, 'harmonics': 2}])
# fitting model to train data
model_results = model.fit()

# printing statsmodels summary for model
print(model_results.summary())

I got the following error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).
</code>
<code>ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data). </code>
ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).

I tried to convert the date column from string to date but with no success.

The np.asarray(df) returns the following

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>array([['2021-01-04 04:00:00-05:00', 133.740000]
['2021-01-04 05:00:00-05:00', 133.600000]
['2021-01-04 06:00:00-05:00', 134.220000]
...
['2021-31-12 17:00:00-05:00', 177.980000]], dtype = object)
</code>
<code>array([['2021-01-04 04:00:00-05:00', 133.740000] ['2021-01-04 05:00:00-05:00', 133.600000] ['2021-01-04 06:00:00-05:00', 134.220000] ... ['2021-31-12 17:00:00-05:00', 177.980000]], dtype = object) </code>
array([['2021-01-04 04:00:00-05:00', 133.740000]
       ['2021-01-04 05:00:00-05:00', 133.600000]
       ['2021-01-04 06:00:00-05:00', 134.220000]
       ...
       ['2021-31-12 17:00:00-05:00', 177.980000]], dtype = object)

I do not know if it is a problem with the date or close column and what to do to fix it.

3

If the issue still persists after using df['date'] = pd.to_datetime(df['date']), it is possible that there are some values in 'date' column which are not in 'datetime' dtype. You can identify those values by the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df['date'] = pd.to_datetime(df['date'], errors='coerce', utc=True)
</code>
<code>df['date'] = pd.to_datetime(df['date'], errors='coerce', utc=True) </code>
df['date'] = pd.to_datetime(df['date'], errors='coerce', utc=True)

This will set those values to NaT(Not a Time). After this you can handle the missing values.

New contributor

Rutuja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật