Skip to content

Keep label case and single periods in default BTable/DTable notes - #132

Open
soodoku wants to merge 1 commit into
py-econometrics:mainfrom
finite-sample:fix/btable-notes-labels
Open

soodoku wants to merge 1 commit into
py-econometrics:mainfrom
finite-sample:fix/btable-notes-labels

Conversation

@soodoku

@soodoku soodoku commented Nov 8, 2026

Copy link
Copy Markdown

What problem do you want to solve?

The default BTable note changes the case of user labels and ends sentences with doubled periods, or with none.

import numpy as np, pandas as pd
from maketables import BTable, DTable

rng = np.random.default_rng(0); n = 200
df = pd.DataFrame({"g": rng.integers(0, 2, n), "g3": rng.integers(0, 3, n),
                   "state": rng.integers(0, 10, n), "x": rng.normal(size=n)})
BTable(df, vars=["x"], group="g", vcov={"CRV1": "state"}, labels={"state": "US State"}).notes
BTable(df, vars=["x"], group="g", vcov="hetero").notes
BTable(df, vars=["x"], group="g", hide_stats=True, vcov="hetero").notes
BTable(df, vars=["x"], group="g3", fixed_effects=["state"], vcov={"CRV1": "state"},
       labels={"state": "US State"}).notes
DTable(df, vars=["x"], bycol=["g"], hide_stats=True).notes
case main expected
CRV1 + label ...clustered on us state. ...clustered on US State.
hetero ...robust standard errors.. ...robust standard errors.
hide_stats + hetero Displayed statistics are mean, std. dev.. p-values based on robust standard errors.. Displayed statistics are Mean, Std. Dev. p-values based on robust standard errors.
FE + CRV1 ...fixed effects and standard errors clustered on us state (no final period) ...clustered on US State.
DTable hide_stats ...N, Mean, Std. Dev.. ...N, Mean, Std. Dev.

(The FE example uses three groups. With two groups and fixed_effects, main currently raises an IndexError, which is handled separately.)

Fix

  • The final n[0] + n[1:].lower() is removed. It lower-cased the whole note after its first character, including user labels and statistic names. Labels now appear exactly as in the table, e.g. "Mean" and "Std. Dev.".
  • A small helper, _end_sentence in dtable.py, adds a period only when the text does not already end with one. The BTable statistics sentence, the BTable p-value sentences and the DTable note now all end through it.
  • "robust standard errors." loses its built-in period. It was the cause of the .. in the hetero case.

The edits are limited to the notes code (btable.py around the default-notes block, and one expression in dtable.py). They do not touch the p-value loop or the fixed_effects docstring.

Tests

tests/api/test_dtable_btable.py:

  • New test_default_notes_keep_label_case_and_single_period, parametrized over the four BTable cases above, compares the full note string.
  • test_bycol_hide_stats_notes (DTable) now compares the full note, so the doubled period is caught.
  • Two existing assertions encoded the lower-casing and are updated: "clustered on hgroup" becomes "clustered on Hgroup" (the label passed is "Hgroup"), and endswith("mean.") becomes endswith("Mean.").

On main, the 4 new cases and the 3 tightened or updated assertions fail. All 42 tests in the file pass with the fix.

  • Full suite: 502 passed. No snapshot changes: no snapshot contains a BTable or DTable default note.
  • pre-commit run --files <changed> and ty check are clean.

Open question

Keeping "Mean" and "Std. Dev." capitalised mid-sentence follows the labels shown in the table. If you prefer lower-case built-in statistic names in the note, that can be done for the built-in names only, without touching user labels.

🤖 Generated with Claude Code

The default BTable note was built from sentence chunks and then passed
through n[0] + n[1:].lower(), which lower-cased user labels
("US State" -> "us state") and the statistic names. Separately, the
robust-SE phrase already ended in "." before another "." was appended
("robust standard errors.."), a statistic list ending in "Std. Dev."
got a second period in both BTable and DTable, and the fixed effects +
clustered SE sentence had no final period.

Drop the lower-casing and end every sentence through one helper,
_end_sentence, which adds a period only when the text does not already
end with one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant