I want to build a continuous table of dates for each customer.
lets suppose I have this data frame
con = pyodbc.connect (....)
The reason why I taking dateadd(day,-1,getdate()) is beacuse there is no data in the table for getdate() only for yesterday.
SQL_Until_Today = pd.read_sql_query("Select date, customer,value from account where date < convert(date,dateadd(day,-1,getdate()))", con)
account = pd.dataframe(SQL_Until_Today , columns = ['date','customer','value'])
SQL_Today = pd.read_sql_query("Select date, customer,value from account where date = convert(date,dateadd(day,-1,getdate()))",con)
account_Today = pd.dataframe(SQL_Today,columns =
['date', 'customer','value'])
account = account.append(account_Today)
So From those two I end up with a dataframe named account who looks something like this:
date customer value
2019-06-27 100 40
2019-06-28 100 30
2019-06-30 100 20
2019-07-01 100 10
2019-07-02 100 18
2019-06-21 200 460
2019-06-23 200 430
2019-06-24 200 410
2019-06-25 200 130
2019-06-26 200 210
2019-06-27 200 410
2019-06-28 200 310
2019-06-30 200 210
2019-07-01 200 110
2019-07-02 200 118
I need to create a continuous table of dates for each customer starting from the min_date he has in the table.
For example:
customer = 100 --> 2019-06-27
customer = 200 --> 2019-06-21
so my desired output for account dataframe will be :
date customer value
2019-06-27 100 40
2019-06-28 100 30
2019-06-29 100 30 *************** The most closer value before!
2019-06-30 100 20
2019-07-01 100 10
2019-07-02 100 18
2019-07-03 100 18 **************** The most closer value before!
2019-06-21 200 460
2019-06-22 200 460 *************** The most closer value before!
2019-06-23 200 430
2019-06-24 200 410
2019-06-25 200 130
2019-06-26 200 210
2019-06-27 200 410
2019-06-28 200 310
2019-06-29 200 310 *************** The most closer value before!
2019-06-30 200 210
2019-07-01 200 110
2019-07-02 200 118
2019-07-03 200 118 *************** The most closer value before!
If there will be a gap of two dates, still I will want to take the value from the most closer date.
Any help how can I perform it effectively?