Metadata-Version: 2.1
Name: create-vars
Version: 0.0.1
Summary: Create variables in time
Home-page: https://github.com/ravennaro/create_vars
Author: Ravenna Oliveira
Author-email: ravenna.rro@gmail.com
License: Apache Software License 2.0
Keywords: nbdev jupyter notebook python
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: License :: OSI Approved :: Apache Software License
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Provides-Extra: dev
License-File: LICENSE

# create_vars

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

``` python
from create_vars.vars import *
```

``` python
import pandas as pd
import random
import numpy as np
```

Criando variáveis numéricas e categóricas

## Install

``` sh
pip install create_vars
```

## How to use

### Criando dataframe aleatório

Criando DataFrame com variáveis categóricas e numéricas

``` python
# Criando o DataFrame
data = {
    'ID_cliente': [random.choice(list(range(1, 101)))for _ in range(100)],
    'Safra': [random.choice([202207, 202209, 202212, 202301, 202207, 202302, 202305, 202306]) for _ in range(100)],
    'Feat_cat': [random.choice(['A', 'B', 'C']) for _ in range(100)],    
    'Feat_num1': np.random.randint(0, 100, size=100),
    'Feat_num2': np.random.randint(0, 100, size=100)
}
df = pd.DataFrame(data)
```

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

<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>

|     | ID_cliente | Safra  | Feat_cat | Feat_num1 | Feat_num2 |
|-----|------------|--------|----------|-----------|-----------|
| 0   | 75         | 202207 | A        | 72        | 66        |
| 1   | 30         | 202209 | B        | 44        | 90        |
| 2   | 70         | 202301 | B        | 82        | 33        |
| 3   | 76         | 202302 | A        | 37        | 70        |
| 4   | 81         | 202305 | C        | 76        | 17        |

</div>

O Dataframe criado tem o ID do cliente aleatório, que pode se repetir em
datas diferentes.Por exemplo:

``` python
df.groupby('ID_cliente')['Safra'].value_counts().sort_values(ascending=False)
```

    ID_cliente  Safra 
    11          202207    2
    70          202301    2
    3           202305    1
    73          202209    1
    79          202207    1
                         ..
    35          202302    1
    34          202305    1
                202306    1
    33          202306    1
    99          202306    1
    Name: count, Length: 98, dtype: int64

A safra corresponde a data que cada variável foi calculada. Ela precisa
estar no formato YYYYMM ou YYYYMMDD. Para o nosso exemplo teremos a
seguinte distribuição das datas:

``` python
df['Safra'].value_counts().sort_index()
```

    Safra
    202207    23
    202209    12
    202212     5
    202301    13
    202302    15
    202305    15
    202306    17
    Name: count, dtype: int64

### Safra de ref

Criando safra de referencia no formato YYYYMM:

``` python
df['safra_ref'] = '20230702'
```

Também podemos utilizar o formato YYYYMMDD.

### Variáveis numéricas

A partir do ID_cliente do cliente e da Safra, agrupamos as entradas e
calculamos variáveis do tipo ‘sum’, ‘mean’ e ‘count’ que são fornecidas
em forma de lista em ‘operations’ para variáveis numéricas. As variáveis
utilizadas são listadas em ‘value_var’ e calculadas nas janelas de tempo
em ‘window’ para a data de referência em ‘ref_time_var’.

``` python
id_cols = ['ID_cliente','Safra']
trns_time = 'Safra'
ref_time = 'safra_ref'
value_var = ['Feat_num1','Feat_num2']
window = [-1,-2,-3,-6,-9,-12,-15]
operations = ['sum','mean','count']

df_vars_num = create_vars_in_time(df,id_cols,trns_time,ref_time,value_var,window,operations)
```

``` python
df_vars_num.head()
```

<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>

|     | ID_cliente | Safra  | Feat_num1_sum_1M | Feat_num1_mean_1M | Feat_num1_count_1M | Feat_num1_sum_2M | Feat_num1_mean_2M | Feat_num1_count_2M | Feat_num1_sum_3M | Feat_num1_mean_3M | ... | Feat_num2_count_6M | Feat_num2_sum_9M | Feat_num2_mean_9M | Feat_num2_count_9M | Feat_num2_sum_12M | Feat_num2_mean_12M | Feat_num2_count_12M | Feat_num2_sum_15M | Feat_num2_mean_15M | Feat_num2_count_15M |
|-----|------------|--------|------------------|-------------------|--------------------|------------------|-------------------|--------------------|------------------|-------------------|-----|--------------------|------------------|-------------------|--------------------|-------------------|--------------------|---------------------|-------------------|--------------------|---------------------|
| 0   | 75         | 202207 | NaN              | NaN               | NaN                | NaN              | NaN               | NaN                | NaN              | NaN               | ... | NaN                | NaN              | NaN               | NaN                | NaN               | NaN                | NaN                 | 66                | 66.0               | 1                   |
| 1   | 30         | 202209 | NaN              | NaN               | NaN                | NaN              | NaN               | NaN                | NaN              | NaN               | ... | NaN                | NaN              | NaN               | NaN                | 90.0              | 90.0               | 1.0                 | 90                | 90.0               | 1                   |
| 2   | 70         | 202301 | NaN              | NaN               | NaN                | NaN              | NaN               | NaN                | NaN              | NaN               | ... | NaN                | 111.0            | 55.5              | 2.0                | 111.0             | 55.5               | 2.0                 | 111               | 55.5               | 2                   |
| 3   | 76         | 202302 | NaN              | NaN               | NaN                | NaN              | NaN               | NaN                | NaN              | NaN               | ... | 1.0                | 70.0             | 70.0              | 1.0                | 70.0              | 70.0               | 1.0                 | 70                | 70.0               | 1                   |
| 4   | 81         | 202305 | NaN              | NaN               | NaN                | NaN              | NaN               | NaN                | 76.0             | 76.0              | ... | 1.0                | 17.0             | 17.0              | 1.0                | 17.0              | 17.0               | 1.0                 | 17                | 17.0               | 1                   |

<p>5 rows × 44 columns</p>
</div>

### Variáveis categóricas

Para variáveis categóricas, as operações são ‘nunique’ e mode.

``` python
id_cols = ['ID_cliente','Safra']
trns_time = 'Safra'
ref_time = 'safra_ref'
value_var = ['Feat_cat']
window = [-1,-2,-3,-6,-9,-12,-15]
operations = ['nunique', mode]

df_vars_cat = create_vars_in_time(df,id_cols,trns_time,ref_time,value_var,window,operations)
```

``` python
df_vars_cat.head()
```

<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>

|     | ID_cliente | Safra  | Feat_cat_nunique_1M | Feat_cat_mode_1M | Feat_cat_nunique_2M | Feat_cat_mode_2M | Feat_cat_nunique_3M | Feat_cat_mode_3M | Feat_cat_nunique_6M | Feat_cat_mode_6M | Feat_cat_nunique_9M | Feat_cat_mode_9M | Feat_cat_nunique_12M | Feat_cat_mode_12M | Feat_cat_nunique_15M | Feat_cat_mode_15M |
|-----|------------|--------|---------------------|------------------|---------------------|------------------|---------------------|------------------|---------------------|------------------|---------------------|------------------|----------------------|-------------------|----------------------|-------------------|
| 0   | 75         | 202207 | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | NaN                  | NaN               | 1                    | A                 |
| 1   | 30         | 202209 | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | 1.0                  | B                 | 1                    | B                 |
| 2   | 70         | 202301 | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | 2.0                 | A                | 2.0                  | A                 | 2                    | A                 |
| 3   | 76         | 202302 | NaN                 | NaN              | NaN                 | NaN              | NaN                 | NaN              | 1.0                 | A                | 1.0                 | A                | 1.0                  | A                 | 1                    | A                 |
| 4   | 81         | 202305 | NaN                 | NaN              | NaN                 | NaN              | 1.0                 | C                | 1.0                 | C                | 1.0                 | C                | 1.0                  | C                 | 1                    | C                 |

</div>
