# core


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

# BFrame

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L20"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame

``` python
def BFrame(
    *values, keys:NoneType=None, names:NoneType=None, **fields
):
```

*A native BPL value with shape, keys and names. An array that holds a
function as a direct item is a `FunctionArray`.*

``` python
test_frame = BFrame(dict(col=[1,2,3]))
test_frame
```

    ┌→────────────┐
    │ col:┌→────┐ │
    │     │1 2 3│ │
    │     └ₓ────┘ │
    └∊────────────┘

``` python
test_eq(BFrame, type(test_frame))
```

## Reading and writing

### CSV

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L24"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.read_csv

``` python
def read_csv(
    path, **kwargs
):
```

``` python
bf = BFrame.read_csv("../data/toy.csv", encoding="UTF-8")
bf.df.head(3)
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Number</th>
<th data-quarto-table-cell-role="th">City</th>
<th data-quarto-table-cell-role="th">Gender</th>
<th data-quarto-table-cell-role="th">Age</th>
<th data-quarto-table-cell-role="th">Income</th>
<th data-quarto-table-cell-role="th">Illness</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>1</td>
<td>Dallas</td>
<td>Male</td>
<td>41</td>
<td>40367.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>2</td>
<td>Dallas</td>
<td>Male</td>
<td>54</td>
<td>45084.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">2</td>
<td>3</td>
<td>Dallas</td>
<td>Male</td>
<td>42</td>
<td>52483.0</td>
<td>No</td>
</tr>
</tbody>
</table>

</div>

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L28"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.save_csv

``` python
def save_csv(
    path
):
```

``` python
# bf.save_csv("../data/save_test.csv");
```

## Selection

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L32"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.take

``` python
def take(
    n:int
):
```

``` python
head = bf.take(100)
head.take(2).df
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Number</th>
<th data-quarto-table-cell-role="th">City</th>
<th data-quarto-table-cell-role="th">Gender</th>
<th data-quarto-table-cell-role="th">Age</th>
<th data-quarto-table-cell-role="th">Income</th>
<th data-quarto-table-cell-role="th">Illness</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>1</td>
<td>Dallas</td>
<td>Male</td>
<td>41</td>
<td>40367.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>2</td>
<td>Dallas</td>
<td>Male</td>
<td>54</td>
<td>45084.0</td>
<td>No</td>
</tr>
</tbody>
</table>

</div>

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L36"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.head

``` python
def head(
    n:int=5
):
```

``` python
bf.head().df
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Number</th>
<th data-quarto-table-cell-role="th">City</th>
<th data-quarto-table-cell-role="th">Gender</th>
<th data-quarto-table-cell-role="th">Age</th>
<th data-quarto-table-cell-role="th">Income</th>
<th data-quarto-table-cell-role="th">Illness</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>1</td>
<td>Dallas</td>
<td>Male</td>
<td>41</td>
<td>40367.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>2</td>
<td>Dallas</td>
<td>Male</td>
<td>54</td>
<td>45084.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">2</td>
<td>3</td>
<td>Dallas</td>
<td>Male</td>
<td>42</td>
<td>52483.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">3</td>
<td>4</td>
<td>Dallas</td>
<td>Male</td>
<td>40</td>
<td>40941.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">4</td>
<td>5</td>
<td>Dallas</td>
<td>Male</td>
<td>46</td>
<td>50289.0</td>
<td>No</td>
</tr>
</tbody>
</table>

</div>

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L40"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.tail

``` python
def tail(
    n:int=5
):
```

``` python
bf.tail().df
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Number</th>
<th data-quarto-table-cell-role="th">City</th>
<th data-quarto-table-cell-role="th">Gender</th>
<th data-quarto-table-cell-role="th">Age</th>
<th data-quarto-table-cell-role="th">Income</th>
<th data-quarto-table-cell-role="th">Illness</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>149996</td>
<td>Austin</td>
<td>Male</td>
<td>48</td>
<td>93669.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>149997</td>
<td>Austin</td>
<td>Male</td>
<td>25</td>
<td>96748.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">2</td>
<td>149998</td>
<td>Austin</td>
<td>Male</td>
<td>26</td>
<td>111885.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">3</td>
<td>149999</td>
<td>Austin</td>
<td>Male</td>
<td>25</td>
<td>111878.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">4</td>
<td>150000</td>
<td>Austin</td>
<td>Female</td>
<td>37</td>
<td>87251.0</td>
<td>No</td>
</tr>
</tbody>
</table>

</div>

## Agg

``` python
MEAN = add.reduce_first / tally
MEAN(bf.head(100)["Income"])
```

    44362.88

## Groupby

``` python
fanout
```

    ↣(□, {f←↑⍺⋄f ⍵}⍤[0 ∞]⍤[∞ 2])

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L49"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.matrix

``` python
def matrix(
    cols
):
```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L53"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.groupby

``` python
def groupby(
    by, cols, **aggs
):
```

``` python
RMSE = MEAN.under(square)
fn_dict=dict(mean=MEAN, rmse=RMSE)
fn_dict
```

    {'mean': +⌿÷≢, 'rmse': (+⌿÷≢)@⊼}

``` python
head.groupby("Gender", "Income", rmse=RMSE)
```

    ┌→───────────────┐
    ↓"Male"⌷         │
    ↓          Income│
    │rmse 49632.10639│
    │                │
    │"Female"⌷       │
    │          Income│
    │rmse 40738.65853│
    └~───────────────┘

``` python
head.groupby("Gender", ["Age", "Income"], **fn_dict)
```

    ┌→───────────────────────────┐
    ↓"Male"⌷                     │
    ↓             Age      Income│
    │mean 44.57407407 48301.42593│
    │rmse 45.66970793 49632.10639│
    │                            │
    │"Female"⌷                   │
    │             Age      Income│
    │mean 45.30434783 39739.36957│
    │rmse 46.73468128 40738.65853│
    └~───────────────────────────┘

## Rolling

`windows` docs with arg explanations:
https://answerdotai.github.io/basedpl/glyphs/up-down-arrow.html

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L60"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.rolling

``` python
def rolling(
    cols, size, edge:str='fill', step:int=1, fill:int=0, **aggs
):
```

``` python
ts = BFrame(dict(price=[5,4.3,4.1,2,10,20,15,25]))
ts.rolling("price", size=3, mean=MEAN, rmse=RMSE).df
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">price</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">0</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>3.100000</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>3.807449</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">1</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>4.466667</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>4.483302</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">2</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>3.466667</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>3.619392</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">3</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>5.366667</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>6.345865</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">4</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>10.666667</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>12.961481</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">5</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>15.000000</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>15.545632</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">6</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>20.000000</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>20.412415</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">7</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>13.333333</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>16.832508</td>
</tr>
</tbody>
</table>

</div>

``` python
ts.rolling("price", size=4, step=2, mean=MEAN, rmse=RMSE).df
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">price</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">0</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>3.350000</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>3.882654</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">1</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>5.100000</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>5.901271</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">2</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>11.750000</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>13.500000</td>
</tr>
<tr>
<td rowspan="2" data-quarto-table-cell-role="th"
data-valign="top">3</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>15.000000</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">rmse</td>
<td>17.677670</td>
</tr>
</tbody>
</table>

</div>

## Viz

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L66"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.plot

``` python
def plot(
    **kwargs
):
```

``` python
BFrame(dict(price=[5,4.3,4.1,2,10,20,15,25])).plot(mark="line", title="Price over time")
```

![](00_core_files/figure-commonmark/cell-26-output-1.svg)

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L70"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.hist_input

``` python
def hist_input(
    n:int
):
```

``` python
income = head["Income"]
income.hist_input(5).df
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">x</th>
<th data-quarto-table-cell-role="th">count</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>21417.9</td>
<td>6</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>31917.7</td>
<td>21</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">2</td>
<td>42417.5</td>
<td>27</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">3</td>
<td>52917.3</td>
<td>36</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">4</td>
<td>63417.1</td>
<td>10</td>
</tr>
</tbody>
</table>

</div>

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/BasedTab/blob/main/basedtab/core.py#L79"
target="_blank" style="float:right; font-size:smaller">source</a>

### BFrame.hist

``` python
def hist(
    n:int=50, **kwargs
):
```

``` python
bf["Income"].hist()
```

![](00_core_files/figure-commonmark/cell-30-output-1.svg)
