Hello I am trying to create a Scatter Plot With Trendline and getting an error due to dtypes in Regression Model of plotly.
My Code:
if "Date1" in dataframe.columns:
dataframe["Date1"] = pd.to_datetime(dataframe["Date1"])
print(dataframe[self.independent_attrib].dtype)
chart = px.scatter(
dataframe,
x=self.dependent_attrib,
y=self.independent_attrib,
trendline="ols",
trendline_color_override="black",
color_discrete_sequence=[colors[0]],
)
Keep in mind that the column “Date1” is equal to self.independent_attrib.
The Error I am getting:
ufunc ‘subtract’ cannot use operands with types dtype(‘<M8[ns]’) and dtype(‘O’)
AIMPED
January 30, 2024, 10:22am
2
Did you check the dtype
of your columns?
The dtype of my column was datetime64
AIMPED
January 30, 2024, 11:04am
4
Could you provide a MRE? I can’t reproduce this:
import plotly.express as px
df = px.data.stocks(datetimes=True)
fig = px.scatter(df, x="date", y="GOOG", trendline="ols")
fig.show()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 105 entries, 0 to 104
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 105 non-null datetime64[ns]
1 GOOG 105 non-null float64
2 AAPL 105 non-null float64
3 AMZN 105 non-null float64
4 FB 105 non-null float64
5 NFLX 105 non-null float64
6 MSFT 105 non-null float64
dtypes: datetime64[ns](1), float64(6)
memory usage: 5.9 KB
My date column is actually of str dtype by default and I am first converting it into datetime type:
if 'Date1' in df.columns:
df['Date1'] = pd.to_datetime(df['Date1']).array
chart = px.scatter(df, x=quantitativeDropdown_value, y=qualitativeDropdown_value, trendline='ols',
trendline_color_override='black',
color_discrete_sequence=[colors[0]])
chart.update_layout(template='simple_white', title=qualitativeDropdown_value,xaxis_rangeslider_visible=False, xaxis=dict(
rangeslider=dict(
visible=True,
bordercolor="black",
borderwidth=1
),
))
And I am getting the error:
The behavior of DatetimeProperties.to_pydatetime is deprecated, in a future version this will return a Series containing python datetime objects instead of an ndarray. To retain the old behavior, call np.array
on the result
ufunc ‘subtract’ cannot use operands with types dtype(‘<M8[ns]’) and dtype(‘O’)