# Buggy code
def __init__(
    self,
    endog,
    exog,
    offset=None,
    exposure=None,
    missing="none",
    check_rank=True,
    **kwargs,
):
    super().__init__(
        endog,
        exog,
        check_rank,
        missing=missing,
        offset=offset,
        exposure=exposure,
        **kwargs,
    )
    if exposure is not None:
        self.exposure = np.log(self.exposure)
    self._check_inputs(self.offset, self.exposure, self.endog)
    if offset is None:
        delattr(self, "offset")
    if exposure is None:
        delattr(self, "exposure")

    # promote dtype to float64 if needed
    dt = np.promote_types(self.endog.dtype, np.float64)
    self.endog = np.asarray(self.endog, dt)
    dt = np.promote_types(self.exog.dtype, np.float64)
    self.exog = np.asarray(self.exog, dt)


# Traceback (ValueError)
ValueError                                Traceback (most recent call last)
<ipython-input-8-623bd13d22ad> in <module>
----> 1 Pmodel2 = sm.Poisson(endog=y_mat, exog=X_mat, exposure=data['PERSONS']).fit() # this gives an error

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/statsmodels/discrete/discrete_model.py in fit(self, start_params, method, maxiter, full_output, disp, callback, **kwargs)
1104                                              disp=disp,
1105                                              callback=callback,
-> 1106                                              **kwargs)
1107
1108         if 'cov_type' in kwargs:

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/statsmodels/discrete/discrete_model.py in fit(self, start_params, method, maxiter, full_output, disp, callback, **kwargs)
231                              disp=disp,
232                              callback=callback,
--> 233                              **kwargs)
234
235         return mlefit  # It is up to subclasses to wrap results

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/statsmodels/base/model.py in fit(self, start_params, method, maxiter, full_output, disp, fargs, callback, retall, skip_hessian, **kwargs)
525                                                        callback=callback,
526                                                        retall=retall,
--> 527                                                        full_output=full_output)
528
529         # NOTE: this is for fit_regularized and should be generalized

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/statsmodels/base/optimizer.py in _fit(self, objective, gradient, start_params, fargs, kwargs, hessian, method, maxiter, full_output, disp, callback, retall)
216                             disp=disp, maxiter=maxiter, callback=callback,
217                             retall=retall, full_output=full_output,
--> 218                             hess=hessian)
219
220         optim_settings = {'optimizer': method, 'start_params': start_params,

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/statsmodels/base/optimizer.py in _fit_newton(f, score, start_params, fargs, kwargs, disp, maxiter, callback, retall, full_output, hess, ridge_factor)
314     while (iterations < maxiter and np.any(np.abs(newparams -
315             oldparams) > tol)):
--> 316         H = np.asarray(hess(newparams))
317         # regularize Hessian, not clear what ridge factor should be
318         # keyword option with absolute default 1e-10, see #1847

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/statsmodels/base/model.py in hess(params, *args)
507
508             def hess(params, *args):
--> 509                 return self.hessian(params, *args) / nobs
510         else:
511             def score(params, *args):

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/statsmodels/discrete/discrete_model.py in hessian(self, params)
1317         X = self.exog
1318         L = np.exp(np.dot(X,params) + exposure + offset)
-> 1319         return -np.dot(L*X.T, X)
1320
1321     def hessian_factor(self, params):

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/pandas/core/ops/common.py in new_method(self, other)
63         other = item_from_zerodim(other)
64
---> 65         return method(self, other)
66
67     return new_method

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/pandas/core/ops/__init__.py in wrapper(left, right)
343         result = arithmetic_op(lvalues, rvalues, op)
344
--> 345         return left._construct_result(result, name=res_name)
346
347     wrapper.__name__ = op_name

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/pandas/core/series.py in _construct_result(self, result, name)
2755         # We do not pass dtype to ensure that the Series constructor
2756         #  does inference in the case where `result` has object-dtype.
-> 2757         out = self._constructor(result, index=self.index)
2758         out = out.__finalize__(self)
2759

~/miniconda3/envs/stdsci/lib/python3.6/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
312                     if len(index) != len(data):
313                         raise ValueError(
--> 314                             f"Length of passed values is {len(data)}, "
315                             f"index implies {len(index)}."
316                         )

ValueError: Length of passed values is 2, index implies 250.

# Fixed code
def __init__(
    self,
    endog,
    exog,
    offset=None,
    exposure=None,
    missing="none",
    check_rank=True,
    **kwargs,
):
    super().__init__(
        endog,
        exog,
        check_rank,
        missing=missing,
        offset=offset,
        exposure=exposure,
        **kwargs,
    )
    if exposure is not None:
        self.exposure = np.asarray(self.exposure)
        self.exposure = np.log(self.exposure)
    if offset is not None:
        self.offset = np.asarray(self.offset)
    self._check_inputs(self.offset, self.exposure, self.endog)
    if offset is None:
        delattr(self, "offset")
    if exposure is None:
        delattr(self, "exposure")

    # promote dtype to float64 if needed
    dt = np.promote_types(self.endog.dtype, np.float64)
    self.endog = np.asarray(self.endog, dt)
    dt = np.promote_types(self.exog.dtype, np.float64)
    self.exog = np.asarray(self.exog, dt)