You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/stats.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -263,6 +263,37 @@ The MLB Stats API publishes the available values directly:
263
263
264
264
Common stat groups include `hitting`, `pitching`, and `fielding`. Available stat types depend on the group and endpoint. Examples include `season`, `career`, `seasonAdvanced`, `gameLog`, and `playLog`.
265
265
266
+
## Numeric stat fields are typed as `float`
267
+
268
+
Rate and average stats such as `avg`, `obp`, `slg`, `ops`, `era`, `whip`, and
269
+
`babip` are typed as `Optional[float]`. The MLB Stats API returns these as
270
+
decimal strings (for example `".287"`), and Pydantic converts them to floats
271
+
automatically:
272
+
273
+
```python
274
+
split.stat.avg ==0.287# not ".287"
275
+
split.stat.model_dump()["avg"] # 0.287, not ".287"
276
+
```
277
+
278
+
This is a behavioral change from earlier releases where these fields were
279
+
`str`. Code relying on string operations (`avg.startswith(".")`) or on
280
+
`avg == ".287"` needs to switch to numeric comparisons.
281
+
282
+
The MLB Stats API also uses two placeholder strings, `".---"` and `"-.--"`,
283
+
for these rate stats when the underlying value is not applicable (for
284
+
example a caught-stealing percentage when nobody has attempted a steal).
285
+
These two known sentinels are normalized to `None` before conversion, so
286
+
`split.stat.avg` is `None` rather than raising a validation error. Any other
287
+
non-numeric string still raises a `ValidationError`, since it isn't a
288
+
sentinel MLB is known to send.
289
+
290
+
Fields that use MLB's innings notation remain `str`, because values like
291
+
`"6.2"` mean 6 2/3 innings rather than the decimal 6.2:
0 commit comments