# Data checks

> Find what will hurt a forecast or a prediction before anything is sent, locally and without a key.

`check` looks at your data on your own machine, before anything is sent, and says what it found and how to fix it. It needs no
key and makes no request.

## Time series

With a time column, the data is checked as time series. Passing `h=` also checks there is enough history for that horizon:

```python
import pandas as pd
import yhatlabs

url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/"
temps = pd.read_csv(url + "daily-min-temperatures.csv")

report = yhatlabs.check(temps, h=28, time_col="Date", target="Temp")
print(report)
print(report.issues[["severity", "code", "detail"]].to_string(index=False))
```

Output:

```
DataCheck(timeseries: 1 series, 1 note)
severity               code                                 detail
note missing_timestamps 2 missing timestamps (0% of the grid).
```

```bash
curl -sO https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-min-temperatures.csv
yhatlabs check daily-min-temperatures.csv --h 28 --time-col Date --target Temp
```

Output:

```
DataCheck(timeseries: 1 series, 1 note)
severity               code    where                                 detail                                                                         suggestion
note missing_timestamps series_0 2 missing timestamps (0% of the grid). They are sent as gaps; yh.fill_gaps() fills them from the history before each gap.
```

## Tables

With `target=` and no time column, the data is checked as a table to predict `target` from:

```python
import pandas as pd
import yhatlabs

titanic = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")

report = yhatlabs.check(titanic, target="Survived")
print(report)
print(report.issues[["severity", "code", "where", "detail"]].to_string(index=False))
```

Output:

```
DataCheck(tabular: 12 columns, 2 warnings, 2 notes)
severity             code       where                                             detail
 warning          id_like PassengerId 891 distinct values in 891 rows: looks like an id.
 warning          id_like        Name 891 distinct values in 891 rows: looks like an id.
note high_cardinality      Ticket                          681 distinct text values.
note   mostly_missing       Cabin                                       77% missing.
```

```bash
curl -s -o titanic.csv https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv
yhatlabs check titanic.csv --target Survived
```

Output:

```
DataCheck(tabular: 12 columns, 2 warnings, 2 notes)
severity             code       where                                             detail                                               suggestion
 warning          id_like PassengerId 891 distinct values in 891 rows: looks like an id.          Drop identifier columns; they don't generalise.
 warning          id_like        Name 891 distinct values in 891 rows: looks like an id.          Drop identifier columns; they don't generalise.
note high_cardinality      Ticket                          681 distinct text values. Free text is not modelled; group rare values or drop it.
note   mostly_missing       Cabin                                       77% missing.                Fine to keep; missing values are handled.
```

## What you get

- `.issues`: one row per finding, with `severity` (`error`, `warning` or `note`), `code`, `where`, `detail` and `suggestion`.
- `.profile`: one row per series (start, end, points, frequency, missing values) or per column (type, missing share, distinct
  values).
- `.ok`: `False` when something would make the call fail.
- `.repaired()`, for series: the data after the clean-up `forecast` applies (sorted, duplicates dropped, missing timestamps
  added), so you can see it before sending.

From the terminal, `yhatlabs check` exits with code 2 when it finds an error, so it can stop a pipeline.

## Everything it looks for

| Time series | Tables |
|---|---|
| duplicate rows, or timestamps repeated within a series | a target with a single value |
| mixed or unknown frequencies | columns that copy, or nearly copy, the target |
| missing timestamps and values | id-like columns |
| too little history for the horizon | imbalanced classes |
| extreme values, rare negatives | integer targets that could be classes or a count |
| series that end before the others | constant, mostly empty or free-text columns |
