Skip to content

Commit 546f180

Browse files
committed
BUG: Correct entity and time count
Remove 0 entries Closes #534
1 parent 6ec8547 commit 546f180

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

doc/source/changes/5.0.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
Variables will appear in results in the same order they are entered in
55
the formula string.
66

7+
Since Version 5.1
8+
-----------------
9+
* Fixed a bug that affected the counting of the number of entities and time periods
10+
in panel models (:issue:`534`).
711

812
Version 5.0
913
------------

linearmodels/panel/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def _lstsq(
9191

9292
def panel_structure_stats(ids: IntArray, name: str) -> Series:
9393
bc = np.bincount(ids)
94+
bc = bc[bc > 0]
9495
index = ["mean", "median", "max", "min", "total"]
9596
out = [bc.mean(), np.median(bc), bc.max(), bc.min(), bc.shape[0]]
9697
return Series(out, index=index, name=name)

linearmodels/tests/panel/test_panel_ols.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,3 +1501,33 @@ def test_quoted_var_names():
15011501
d = pd.DataFrame({"var a": np.arange(100.0)}, index=mi)
15021502
res = PanelOLS.from_formula("`var a` ~ 1", data=d).fit()
15031503
assert_allclose(res.params, d.mean().iloc[0])
1504+
1505+
1506+
def test_entity_into():
1507+
# GH 534
1508+
rg = np.random.default_rng(12345)
1509+
mi = pd.MultiIndex.from_product([np.arange(20), np.arange(5)])
1510+
df = pd.DataFrame(
1511+
{
1512+
"a": rg.standard_normal(100),
1513+
"b": rg.standard_normal(100),
1514+
},
1515+
index=mi,
1516+
)
1517+
res = PanelOLS.from_formula("a ~ 1 + b", data=df).fit()
1518+
ei = res.entity_info
1519+
assert ei["total"] == 20
1520+
assert ei["min"] == 5
1521+
ti = res.time_info
1522+
assert ti["total"] == 5
1523+
assert ti["min"] == 20
1524+
1525+
df2 = df.drop([2, 4, 6, 8], level=0)
1526+
df2 = df2.drop(0, level=1)
1527+
res = PanelOLS.from_formula("a ~ 1 + b", data=df2).fit()
1528+
ei = res.entity_info
1529+
assert ei["min"] == 4
1530+
assert ei["total"] == 16
1531+
ti = res.time_info
1532+
assert ti["total"] == 4
1533+
assert ti["min"] == 16

0 commit comments

Comments
 (0)