Skip to content

Commit ad35f85

Browse files
authored
Merge pull request statsmodels#8797 from bashtage/improve-warning-check
MAINT: Improve specificity of warning check
2 parents 4afdb22 + fe89b56 commit ad35f85

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

statsmodels/compat/pandas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
"get_cached_doc",
3232
"call_cached_func",
3333
"PD_LT_1_4",
34+
"PD_LT_2",
3435
]
3536

3637
version = parse(pd.__version__)
3738

3839
PD_LT_1_0_0 = version < Version("1.0.0")
3940
PD_LT_1_4 = version < Version("1.3.99")
41+
PD_LT_2 = version < Version("1.9.99")
4042

4143
try:
4244
from pandas.api.types import is_numeric_dtype

statsmodels/imputation/tests/test_mice.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def test_settingwithcopywarning(self):
121121
with warnings.catch_warnings(record=True) as ws:
122122
warnings.simplefilter('always')
123123
miceData.update_all()
124-
124+
# Only include pandas warnings. There are many from patsy
125+
# and sometimes warnings from other packages here
126+
ws = [w for w in ws if "\\pandas\\" in w.filename]
125127
assert len(ws) == 0
126128

127129
def test_next_sample(self):

statsmodels/stats/descriptivestats.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
from statsmodels.compat.pandas import Appender, is_numeric_dtype
22
from statsmodels.compat.scipy import SP_LT_19
3-
3+
from statsmodels.compat.pandas import PD_LT_2
44
from typing import Sequence, Union
55

66
import numpy as np
77
import pandas as pd
8-
from pandas.core.dtypes.common import is_categorical_dtype
8+
if PD_LT_2:
9+
from pandas.core.dtypes.common import is_categorical_dtype
10+
else:
11+
# After pandas 2 is the minium, use the isinstance check
12+
def is_categorical_dtype(dtype):
13+
return isinstance(dtype, pd.CategoricalDtype)
14+
915
from scipy import stats
1016

1117
from statsmodels.iolib.table import SimpleTable
@@ -392,10 +398,10 @@ def numeric(self) -> pd.DataFrame:
392398
q = stats.norm.ppf(1.0 - self._alpha / 2)
393399

394400
def _mode(ser):
395-
if SP_LT_19:
396-
mode_res = stats.mode(ser.dropna())
397-
else:
398-
mode_res = stats.mode(ser.dropna(), keepdims=True)
401+
dtype = ser.dtype if isinstance(ser.dtype, np.dtype) else ser.dtype.numpy_dtype
402+
ser_no_missing = ser.dropna().to_numpy(dtype=dtype)
403+
kwargs = {} if SP_LT_19 else {"keepdims": True}
404+
mode_res = stats.mode(ser_no_missing, **kwargs)
399405
# Changes in SciPy 1.10
400406
if np.isscalar(mode_res[0]):
401407
return float(mode_res[0]), mode_res[1]

0 commit comments

Comments
 (0)