I am trying to insert fake data into this table. It can't be totally random because the rows would need to make sense. I'll explain below.
My data looks like this:
| AcctID | account_status | start_date | end_date |
|---|---|---|---|
| C382861922 | ACTIVE | 2016-05-25 | None |
| C382861922 | INACTIVE | None | None |
| C382861922 | ACTIVE | None | None |
| C382861922 | INACTIVE | None | 2021-12-31 |
| C429768513 | ACTIVE | 2015-12-27 | None |
| C429768513 | INACTIVE | None | None |
| C429768513 | ACTIVE | None | None |
| C429768513 | INACTIVE | None | None |
| C429768513 | ACTIVE | None | None |
| C429768513 | INACTIVE | None | None |
| C429768513 | ACTIVE | None | None |
| C429768513 | INACTIVE | None | 2021-12-31 |
| C643625629 | ACTIVE | 2016-07-24 | None |
| C643625629 | INACTIVE | None | None |
| C643625629 | ACTIVE | None | 2021-12-31 |
| C82157435 | ACTIVE | 2016-10-22 | None |
| C82157435 | INACTIVE | None | 2021-12-31 |
Each AcctID can appear multiple times, but it's easiest to explain what I'm doing with just an example where the AcctID appears twice:
| AcctID | account_status | start_date | end_date |
|---|---|---|---|
| C82157435 | ACTIVE | 2016-10-22 | None |
| C82157435 | INACTIVE | None | 2021-12-31 |
My goal is to randomly pick a date where this customer changed their account_status, which would become both the end_date of the first row and the start_date of the 2nd row. So, I only need to pick 1 random date, and insert it in both places. Easy enough - I can max() and min() and then calculate the difference in days, and then choose a random integer within that range.
However, I can't figure out how I'd do it for a customer with using more than 2 records:
| AcctID | account_status | start_date | end_date |
|---|---|---|---|
| C429768513 | ACTIVE | 2015-12-27 | None |
| C429768513 | INACTIVE | None | None |
| C429768513 | ACTIVE | None | None |
| C429768513 | INACTIVE | None | None |
| C429768513 | ACTIVE | None | None |
| C429768513 | INACTIVE | None | None |
| C429768513 | ACTIVE | None | None |
| C429768513 | INACTIVE | None | 2021-12-31 |
There will be several places to choose a random date, but since they need to correspond to each other, the problem becomes really complex. Any ideas?
Here's code to create the sample dataframe:
import pandas as pd
fake = [
{
"AcctID": "C429768513",
"account_status": "ACTIVE",
"start_date": "2015-12-27",
"end_date": "None"
},
{
"AcctID": "C429768513",
"account_status": "INACTIVE",
"start_date": "None",
"end_date": "None"
},
{
"AcctID": "C429768513",
"account_status": "ACTIVE",
"start_date": "None",
"end_date": "None"
},
{
"AcctID": "C429768513",
"account_status": "INACTIVE",
"start_date": "None",
"end_date": "None"
},
{
"AcctID": "C429768513",
"account_status": "ACTIVE",
"start_date": "None",
"end_date": "None"
},
{
"AcctID": "C429768513",
"account_status": "INACTIVE",
"start_date": "None",
"end_date": "None"
},
{
"AcctID": "C429768513",
"account_status": "ACTIVE",
"start_date": "None",
"end_date": "None"
},
{
"AcctID": "C429768513",
"account_status": "INACTIVE",
"start_date": "None",
"end_date": "2021-12-31"
}
]
df = pd.DataFrame(fake)
Edit: Here's a fake example of what the program output could look like. Please note most of the dates are randomly chosen - but the end date of the preceding row matches the start date of the next row.
| AcctID | account_status | start_date | end_date |
|---|---|---|---|
| C429768513 | ACTIVE | 2015-12-27 | 2016-01-05 |
| C429768513 | INACTIVE | 2016-01-05 | 2016-03-01 |
| C429768513 | ACTIVE | 2016-03-01 | 2017-06-22 |
| C429768513 | INACTIVE | 2017-06-22 | 2017-09-04 |
| C429768513 | ACTIVE | 2017-09-04 | 2018-10-27 |
| C429768513 | INACTIVE | 2018-10-27 | 2019-04-04 |
| C429768513 | ACTIVE | 2019-04-04 | 2020-06-06 |
| C429768513 | INACTIVE | 2020-06-06 | 2021-12-31 |