forked from keshavchoudhary87/domestic_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GSTR1_table12.py
164 lines (139 loc) · 8.52 KB
/
GSTR1_table12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pandas as pd
import numpy as np
path = 'E:\GSTN Data\Working\GSTR1\\'
dict_qtr = {1:4, 2:4, 3:4, 4:1, 5:1, 6:1, 7:2, 8:2, 9:2, 10:3, 11:3, 12:3}
files_gstr1_2017 = ['072017_top_10L_R1_TABLE_12.csv', '082017_top_10L_R1_TABLE_12.csv', '092017_top_10L_R1_TABLE_12.csv', '102017_top_10L_R1_TABLE_12.csv',
'112017_top_10L_R1_TABLE_12.csv', '122017_top_10L_R1_TABLE_12.csv', '012018_top_10L_R1_TABLE_12.csv', '022018_top_10L_R1_TABLE_12.csv',
'032018_top_10L_R1_TABLE_12.csv']
files_gstr1_2018 = ['042018_top_10L_R1_TABLE_12.csv', '052018_top_10L_R1_TABLE_12.csv', '062018_top_10L_R1_TABLE_12.csv', '072018_top_10L_R1_TABLE_12.csv',
'082018_top_10L_R1_TABLE_12.csv', '092018_top_10L_R1_TABLE_12.csv', '102018_top_10L_R1_TABLE_12.csv', '112018_top_10L_R1_TABLE_12.csv',
'122018_top_10L_R1_TABLE_12.csv', '012019_top_10L_R1_TABLE_12.csv', '022019_top_10L_R1_TABLE_12.csv', '032019_top_10L_R1_TABLE_12.csv']
'''
Read data from each of the monthly files for FY 2017-18
'''
first_flag = 0
for file in files_gstr1_2017:
file_name = path + file
df_temp = pd.read_csv(file_name, dtype={'state_cd': object, 'rtn_prd': object}, engine='python')
# Add CGST, SGST & IGST liabilities to get combined liabilities
df_temp['tax_liab'] = df_temp.cgst + df_temp.sgst + df_temp.igst
# Segregate month and year from period
df_temp['year'] = df_temp.rtn_prd.str.slice(2,)
df_temp['month'] = df_temp.rtn_prd.str.slice(0, 2)
df_temp['year'] = df_temp['year'].astype(int)
df_temp['month'] = df_temp['month'].astype(int)
# Assign quarter for months based on dict_qtr
df_temp['qtr'] = df_temp['month'].map(dict_qtr)
df_temp['year'] = np.where(df_temp['qtr']==4, df_temp['year']-1, df_temp['year'])
# Append the monthly data into a single dataframe
if first_flag==0:
gstr1_2017 = df_temp
else:
gstr1_2017 = gstr1_2017.append(df_temp, ignore_index=True)
first_flag += 1
# Convert monthly data to quarterly
gstr1_2017['gstin_period_hsn'] = gstr1_2017.ann_gstin_id + '_' + gstr1_2017.year.astype(str) + '_' + gstr1_2017.qtr.astype(str) + '_' + gstr1_2017.hsn_sc
grouped = gstr1_2017.groupby('gstin_period_hsn')['taxable_value', 'cgst', 'sgst', 'igst', 'tax_liab', 'cess'].sum()
qtr_gstr1_2017 = pd.DataFrame(data=grouped)
qtr_gstr1_2017 = qtr_gstr1_2017.reset_index()
qtr_gstr1_2017['gstin_hash'] = qtr_gstr1_2017.gstin_period_hsn.str.slice(0,39)
qtr_gstr1_2017['year'] = qtr_gstr1_2017.gstin_period_hsn.str.slice(40, 44)
qtr_gstr1_2017['qtr'] = qtr_gstr1_2017.gstin_period_hsn.str.slice(45, 46)
qtr_gstr1_2017['hsn'] = qtr_gstr1_2017.gstin_period_hsn.str.slice(47,)
qtr_gstr1_2017 = qtr_gstr1_2017.drop(['gstin_period_hsn'], axis=1)
qtr_gstr1_2017['state_cd'] = qtr_gstr1_2017.gstin_hash.str.slice(0, 2)
# Delete gstr1_2017 to free up memory
del gstr1_2017
# Exporting the quarterly dataframe to a csv file
qtr_gstr1_2017.to_csv('qtr_gstr1_2017.csv', index=False)
# Convert quarterly data to annual
qtr_gstr1_2017['gstin_year_hsn'] = qtr_gstr1_2017.gstin_hash + '_' + qtr_gstr1_2017.year.astype(str) + '_' + qtr_gstr1_2017.hsn
grouped = qtr_gstr1_2017.groupby('gstin_year_hsn')['taxable_value', 'cgst', 'sgst', 'igst', 'tax_liab', 'cess'].sum()
yr_gstr1_2017 = pd.DataFrame(data=grouped)
yr_gstr1_2017 = yr_gstr1_2017.reset_index()
yr_gstr1_2017['gstin_hash'] = yr_gstr1_2017.gstin_year_hsn.str.slice(0,39)
yr_gstr1_2017['year'] = yr_gstr1_2017.gstin_year_hsn.str.slice(40, 44)
yr_gstr1_2017['hsn'] = yr_gstr1_2017.gstin_year_hsn.str.slice(45,)
yr_gstr1_2017 = yr_gstr1_2017.drop(['gstin_year_hsn'], axis=1)
yr_gstr1_2017['state_cd'] = yr_gstr1_2017.gstin_hash.str.slice(0, 2)
# Delete qtr_gstr1_2017 to free up memory
del qtr_gstr1_2017
# Export gstr1_table12 for FY 2017-18
yr_gstr1_2017.to_csv('yr_gstr1_2017.csv', index=False)
'''
Read data from each of the monthly files for FY 2018-19
'''
first_flag = 0
for file in files_gstr1_2018:
file_name = path + file
df_temp = pd.read_csv(file_name, dtype={'state_cd': object, 'rtn_prd': object})
# Add CGST, SGST & IGST liabilities to get combined liabilities
df_temp['tax_liab'] = df_temp.cgst + df_temp.sgst + df_temp.igst
# Segregate month and year from period
df_temp['year'] = df_temp.rtn_prd.str.slice(2,)
df_temp['month'] = df_temp.rtn_prd.str.slice(0, 2)
df_temp['year'] = df_temp['year'].astype(int)
df_temp['month'] = df_temp['month'].astype(int)
# Assign quarter for months based on dict_qtr
df_temp['qtr'] = df_temp['month'].map(dict_qtr)
df_temp['year'] = np.where(df_temp['qtr']==4, df_temp['year']-1, df_temp['year'])
# Append the monthly data into a single dataframe
if first_flag==0:
gstr1_2018 = df_temp
else:
gstr1_2018 = gstr1_2018.append(df_temp, ignore_index=True)
first_flag += 1
# Convert monthly data to quarterly
gstr1_2018['gstin_period_hsn'] = gstr1_2018.ann_gstin_id + '_' + gstr1_2018.year.astype(str) + '_' + gstr1_2018.qtr.astype(str) + '_' + gstr1_2018.hsn_sc
grouped = gstr1_2018.groupby('gstin_period_hsn')['taxable_value', 'cgst', 'sgst', 'igst', 'tax_liab', 'cess'].sum()
qtr_gstr1_2018 = pd.DataFrame(data=grouped)
qtr_gstr1_2018 = qtr_gstr1_2018.reset_index()
qtr_gstr1_2018['gstin_hash'] = qtr_gstr1_2018.gstin_period_hsn.str.slice(0,39)
qtr_gstr1_2018['year'] = qtr_gstr1_2018.gstin_period_hsn.str.slice(40, 44)
qtr_gstr1_2018['qtr'] = qtr_gstr1_2018.gstin_period_hsn.str.slice(45, 46)
qtr_gstr1_2018['hsn'] = qtr_gstr1_2018.gstin_period_hsn.str.slice(47,)
qtr_gstr1_2018 = qtr_gstr1_2018.drop(['gstin_period_hsn'], axis=1)
qtr_gstr1_2018['state_cd'] = qtr_gstr1_2018.gstin_hash.str.slice(0, 2)
# Delete gstr1_2018 to free up memory
del gstr1_2018
# Exporting the quarterly dataframe to a csv file
qtr_gstr1_2018.to_csv('qtr_gstr1_2018.csv', index=False)
# Convert quarterly data to annual
qtr_gstr1_2018['gstin_year_hsn'] = qtr_gstr1_2018.gstin_hash + '_' + qtr_gstr1_2018.year.astype(str) + '_' + qtr_gstr1_2018.hsn
grouped = qtr_gstr1_2018.groupby('gstin_year_hsn')['taxable_value', 'cgst', 'sgst', 'igst', 'tax_liab', 'cess'].sum()
yr_gstr1_2018 = pd.DataFrame(data=grouped)
yr_gstr1_2018 = yr_gstr1_2018.reset_index()
yr_gstr1_2018['gstin_hash'] = yr_gstr1_2018.gstin_year_hsn.str.slice(0,39)
yr_gstr1_2018['year'] = yr_gstr1_2018.gstin_year_hsn.str.slice(40, 44)
yr_gstr1_2018['hsn'] = yr_gstr1_2018.gstin_year_hsn.str.slice(45,)
yr_gstr1_2018 = yr_gstr1_2018.drop(['gstin_year_hsn'], axis=1)
yr_gstr1_2018['state_cd'] = yr_gstr1_2018.gstin_hash.str.slice(0, 2)
yr_gstr1_2018['hsn_len'] = yr_gstr1_2018.hsn.str.len()
yr_gstr1_2018['chapter'] = yr_gstr1_2018.hsn.str.slice(0, 2)
yr_gstr1_2018['chapter'] = np.where(yr_gstr1_2018.hsn_len==1, '0' + yr_gstr1_2018.hsn.str.slice(0, 1), yr_gstr1_2018.chapter)
yr_gstr1_2018['chapter'] = np.where(yr_gstr1_2018.hsn_len==3, '0' + yr_gstr1_2018.hsn.str.slice(0, 1), yr_gstr1_2018.chapter)
yr_gstr1_2018['chapter'] = np.where(yr_gstr1_2018.hsn_len==5, '0' + yr_gstr1_2018.hsn.str.slice(0, 1), yr_gstr1_2018.chapter)
yr_gstr1_2018['chapter'] = np.where(yr_gstr1_2018.hsn_len==7, '0' + yr_gstr1_2018.hsn.str.slice(0, 1), yr_gstr1_2018.chapter)
yr_gstr1_2018_services = yr_gstr1_2018[yr_gstr1_2018.chapter == '99']
yr_gstr1_2018_err = yr_gstr1_2018[(yr_gstr1_2018.chapter == 'No') | (yr_gstr1_2018.chapter == '00')]
yr_gstr1_2018_goods = yr_gstr1_2018[(yr_gstr1_2018.chapter != 'No') & (yr_gstr1_2018.chapter != '00') & (yr_gstr1_2018.chapter != '99')]
# Delete qtr_gstr1_2018 to free up memory
del qtr_gstr1_2018
# Export gstr1_table12 for FY 2018-19
yr_gstr1_2018.to_csv('yr_gstr1_2018.csv', index=False)
yr_gstr1_2018_services.to_csv('yr_gstr1_2018_services.csv', index=False)
yr_gstr1_2018_err.to_csv('yr_gstr1_2018_err.csv', index=False)
yr_gstr1_2018_goods.to_csv('yr_gstr1_2018_goods.csv', index=False)
'''
Consolidate 2017 and 2018 data
'''
temp_df = yr_gstr1_2017.append(yr_gstr1_2018, ignore_index=True)
temp_df['gstin_hsn'] = temp_df.gstin_hash + '_' + temp_df.hsn
grouped = temp_df.groupby('gstin_hsn')['taxable_value', 'cgst', 'sgst', 'igst', 'tax_liab', 'cess'].sum()
yr_gstr1_all = pd.DataFrame(data=grouped)
yr_gstr1_all = yr_gstr1_all.reset_index()
yr_gstr1_2018['gstin_hash'] = yr_gstr1_2018.gstin_hsn.str.slice(0,39)
yr_gstr1_2018['hsn'] = yr_gstr1_2018.gstin_hash.str.slice(40,)
yr_gstr1_all['state_cd'] = yr_gstr1_all.gstin_hash.str.slice(0, 2)
# Export consolidated gstr1_table12
yr_gstr1_all.to_csv('gstr1_table12.csv', index=False)