Skip to content

Commit 60a6524

Browse files
authored
Merge pull request #647 from bashtage/pandas-3-fixes
MAINT: Fixes for pandas and flake
2 parents 99e06fc + 609d148 commit 60a6524

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

linearmodels/system/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _missing_weights(
102102
missing = [key for key in weights if weights[key] is None]
103103
if missing:
104104
msg = "Weights not found for equation labels:\n{}".format(", ".join(missing))
105-
warnings.warn(msg, UserWarning)
105+
warnings.warn(msg, UserWarning, stacklevel=2)
106106

107107

108108
def _parameters_from_xprod(
@@ -1917,6 +1917,7 @@ def __init__(
19171917
"matrix not unadjusted (homoskedastic). sigma will "
19181918
"be ignored.",
19191919
UserWarning,
1920+
stacklevel=1,
19201921
)
19211922
weight_type = COV_TYPES[weight_type]
19221923
self._weight_est = GMM_W_EST[weight_type](**weight_config)

linearmodels/tests/panel/_utility.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,23 @@ def generate_data(
200200

201201

202202
def assert_results_equal(res1, res2, test_fit=True, test_df=True, strict=True):
203+
from pandas import Index
204+
205+
def fix_index(x: Series | DataFrame, n: int):
206+
if isinstance(x, Series):
207+
out = x.iloc[:n].copy()
208+
else:
209+
out = x.iloc[:n, :n].copy()
210+
out.index = Index(out.index.to_list())
211+
return out
212+
203213
n = min(res1.params.shape[0], res2.params.shape[0])
204214

205-
assert_series_equal(res1.params.iloc[:n], res2.params.iloc[:n])
206-
assert_series_equal(res1.pvalues.iloc[:n], res2.pvalues.iloc[:n])
207-
assert_series_equal(res1.tstats.iloc[:n], res2.tstats.iloc[:n])
208-
assert_frame_equal(res1.cov.iloc[:n, :n], res2.cov.iloc[:n, :n])
209-
assert_frame_equal(res1.conf_int().iloc[:n], res2.conf_int().iloc[:n])
215+
assert_series_equal(res1.params.iloc[:n], fix_index(res2.params, n))
216+
assert_series_equal(res1.pvalues.iloc[:n], fix_index(res2.pvalues, n))
217+
assert_series_equal(res1.tstats.iloc[:n], fix_index(res2.tstats, n))
218+
assert_frame_equal(res1.cov.iloc[:n, :n], fix_index(res2.cov, n))
219+
assert_frame_equal(res1.conf_int().iloc[:n], fix_index(res2.conf_int(), n))
210220

211221
assert_allclose(res1.s2, res2.s2)
212222

linearmodels/tests/panel/test_panel_ols.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ def test_panel_entity_lsdv(data):
286286
d_demean = d.values
287287

288288
xd = np.c_[x.values, d_demean]
289-
xd = pd.DataFrame(xd, index=x.index, columns=list(x.columns) + list(d.columns))
289+
xd = pd.DataFrame(
290+
xd, index=x.index, columns=list(x.columns) + [f"fe_{col}" for col in d.columns]
291+
)
290292

291293
ols_mod = IV2SLS(y, xd, None, None)
292294
res2 = ols_mod.fit(cov_type="unadjusted", debiased=False)
@@ -388,7 +390,9 @@ def test_panel_time_lsdv(large_data):
388390
d = d - z @ lstsq(z, d, rcond=None)[0]
389391

390392
xd = np.c_[x.values, d]
391-
xd = pd.DataFrame(xd, index=x.index, columns=list(x.columns) + d_cols)
393+
xd = pd.DataFrame(
394+
xd, index=x.index, columns=list(x.columns) + [f"fe_{col}" for col in d_cols]
395+
)
392396

393397
ols_mod = IV2SLS(y, xd, None, None)
394398
res2 = ols_mod.fit(cov_type="unadjusted")
@@ -490,7 +494,11 @@ def test_panel_both_lsdv(data):
490494

491495
xd = np.c_[x.values, d]
492496
xd = pd.DataFrame(
493-
xd, index=x.index, columns=list(x.columns) + list(d1.columns) + list(d2.columns)
497+
xd,
498+
index=x.index,
499+
columns=list(x.columns)
500+
+ [f"fe_{col}" for col in d1.columns]
501+
+ [f"te_{col}" for col in d2.columns],
494502
)
495503

496504
ols_mod = IV2SLS(y, xd, None, None)
@@ -597,7 +605,9 @@ def test_panel_entity_lsdv_weighted(data):
597605
d = d - z @ lstsq(wz, wd, rcond=None)[0]
598606

599607
xd = np.c_[x.values, d]
600-
xd = pd.DataFrame(xd, index=x.index, columns=list(x.columns) + list(d_cols))
608+
xd = pd.DataFrame(
609+
xd, index=x.index, columns=list(x.columns) + [f"fe_{col}" for col in d_cols]
610+
)
601611

602612
ols_mod = IV2SLS(y, xd, None, None, weights=w)
603613
res2 = ols_mod.fit(cov_type="unadjusted")
@@ -677,7 +687,9 @@ def test_panel_time_lsdv_weighted(large_data):
677687
d = d - z @ lstsq(wz, wd, rcond=None)[0]
678688

679689
xd = np.c_[x.values, d]
680-
xd = pd.DataFrame(xd, index=x.index, columns=list(x.columns) + list(d_cols))
690+
xd = pd.DataFrame(
691+
xd, index=x.index, columns=list(x.columns) + [f"te_{col}" for col in d_cols]
692+
)
681693

682694
ols_mod = IV2SLS(y, xd, None, None, weights=w)
683695
res2 = ols_mod.fit(cov_type="unadjusted")
@@ -760,7 +772,11 @@ def test_panel_both_lsdv_weighted(data):
760772

761773
xd = np.c_[x.values, d]
762774
xd = pd.DataFrame(
763-
xd, index=x.index, columns=list(x.columns) + list(d1.columns) + list(d2.columns)
775+
xd,
776+
index=x.index,
777+
columns=list(x.columns)
778+
+ [f"fe_{col}" for col in d1.columns]
779+
+ [f"te_{col}" for col in d2.columns],
764780
)
765781

766782
ols_mod = IV2SLS(y, xd, None, None, weights=w)

0 commit comments

Comments
 (0)