frequencies: Unhandled error with missing or invalid dates
bug
### Current Behavior
`augur frequencies` crashes with an unhandled error when any used sequence has a missing or invalid date (e.g. empty string, `XXXX-XX-XX`, or other unrecognized values that `get_numerical_date_from_value()` returns `None` for).
Note: unused sequences (not in the tree/alignment) do not trigger the error, since their dates are never accessed.
### Expected behavior
A clean error or no error.
### How to reproduce
The exact error depends on the mix of date values. `get_numerical_dates()` returns a dict with float values for exact dates, tuple values for ambiguous dates (or date ranges), and `None` for invalid/missing dates.
1. When the dict contains tuples, the underlying array is object-typed and `None` stays as `None`, causing a `TypeError` in `np.mean()`.
2. When all valid dates are exact (floats only), pandas coerces `None` to `NaN`, which passes through `np.mean()` but later causes a `ValueError`.
<details>
<summary>
Scenario 1: with ambiguous dates
</summary>
```sh
cat >tree.nwk <<~~
(A:0.1,B:0.1,C:0.1);
~~
cat >metadata.tsv <<~~
strain date
A invalid
B 2016-01-06
C 2016-04-XX
~~
augur frequencies \
--metadata metadata.tsv \
--tree tree.nwk \
--method kde \
--pivot-interval 3 \
--output out.json
```
Output:
```
File ".../numpy/core/_methods.py", line 131, in _mean
ret = ret / rcount
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
```
</details>
<details>
<summary>
Scenario 2: with exact dates
</summary>
```sh
cat >tree.nwk <<~~
(A:0.1,B:0.1);
~~
cat >metadata.tsv <<~~
strain date
A invalid
B 2016-01-06
~~
augur frequencies \
--metadata metadata.tsv \
--tree tree.nwk \
--method kde \
--pivot-interval 3 \
--output out.json
```
Output:
```
File ".../augur/frequency_estimators.py", line 840, in float_to_datestring
days_in_year = 366 if isleap(int(numdate)) else 365
ValueError: cannot convert float NaN to integer
```
</details>
### Possible solutions
1. Error on missing dates in the tree or alignment. Example from [nextstrain/WNV#118](https://github.com/nextstrain/WNV/issues/118):
```
ERROR: The following sequence ids are missing valid dates:
'AF202541' has date 'XXXX-XX-XX'
'AF206518' has date 'XXXX-XX-XX'
'AF260968' has date 'XXXX-XX-XX'
'AF481864' has date 'XXXX-XX-XX'
```
2. Error on **all** sequences with missing dates, regardless of whether they are in the tree or alignment.
- This is a stricter data quality check that's probably not necessary.
3. Automatically drop sequences with missing dates.
- This could hide underlying data issues and produce unexpected results.
---
First noticed in https://github.com/nextstrain/augur/pull/1913#discussion_r2453394839
关闭于 2026-03-05 0 条评论