Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -192,10 +192,13 @@ def col_to_ints(df,columns_to_convert):
|
|
| 192 |
|
| 193 |
def fill_missing_quarters(df, lob, acc, transaction):
|
| 194 |
print('accident',acc,'transaction',transaction)
|
| 195 |
-
# Create a list of all possible quarters between 2017 and 2022
|
| 196 |
columns_to_convert = [acc,transaction] # Only affect acc and transaction
|
|
|
|
|
|
|
|
|
|
| 197 |
for col in columns_to_convert:
|
| 198 |
df[col] = df[col].apply(lambda x: str(int(x)) if isinstance(x, (int, float)) and str(x) != 'nan' else str(x))
|
|
|
|
| 199 |
quarters = []
|
| 200 |
start_year = 2017
|
| 201 |
end_year = min(int(df[acc].max()[:4]), 2025)
|
|
@@ -222,10 +225,17 @@ def fill_missing_quarters(df, lob, acc, transaction):
|
|
| 222 |
print(l,'was filled with the dates',l_quarters)
|
| 223 |
l_missing_df[lob] = l
|
| 224 |
missing_quarters.append(l_missing_df)
|
|
|
|
| 225 |
# Concatenate the original dataframe and the missing quarters dataframe
|
| 226 |
filled_df = pd.concat([df] + missing_quarters, ignore_index=True)
|
|
|
|
|
|
|
|
|
|
| 227 |
# Convert the 'accident_quarter_bracket' column to datetime format
|
| 228 |
filled_df[acc] = pd.to_datetime(filled_df[acc], format='%Y%m').dt.strftime('%Y%m')
|
|
|
|
|
|
|
|
|
|
| 229 |
# Sort the dataframe by quarter
|
| 230 |
filled_df = filled_df.sort_values(acc)
|
| 231 |
# Reset the index
|
|
@@ -237,6 +247,7 @@ def fill_missing_quarters(df, lob, acc, transaction):
|
|
| 237 |
print("No missing quarters between 2017-2022")
|
| 238 |
else:
|
| 239 |
pass#print(filtered_quarters)
|
|
|
|
| 240 |
#filled_df = filled_df[[acc, transaction] + [col for col in filled_df.columns if col not in [acc, transaction]]]
|
| 241 |
return filled_df
|
| 242 |
|
|
|
|
| 192 |
|
| 193 |
def fill_missing_quarters(df, lob, acc, transaction):
|
| 194 |
print('accident',acc,'transaction',transaction)
|
|
|
|
| 195 |
columns_to_convert = [acc,transaction] # Only affect acc and transaction
|
| 196 |
+
|
| 197 |
+
print('Number of NaN values in', acc, ':', df[acc].isna().sum())
|
| 198 |
+
print('Number of NaN values in', transaction, ':', df[transaction].isna().sum())
|
| 199 |
for col in columns_to_convert:
|
| 200 |
df[col] = df[col].apply(lambda x: str(int(x)) if isinstance(x, (int, float)) and str(x) != 'nan' else str(x))
|
| 201 |
+
|
| 202 |
quarters = []
|
| 203 |
start_year = 2017
|
| 204 |
end_year = min(int(df[acc].max()[:4]), 2025)
|
|
|
|
| 225 |
print(l,'was filled with the dates',l_quarters)
|
| 226 |
l_missing_df[lob] = l
|
| 227 |
missing_quarters.append(l_missing_df)
|
| 228 |
+
print('Unique values in', acc, 'for missing quarters:', l_missing_df[acc].unique())
|
| 229 |
# Concatenate the original dataframe and the missing quarters dataframe
|
| 230 |
filled_df = pd.concat([df] + missing_quarters, ignore_index=True)
|
| 231 |
+
print('Number of NaN values in', acc, 'after concatenation:', filled_df[acc].isna().sum())
|
| 232 |
+
|
| 233 |
+
print('Unique values in', acc, 'before conversion:', filled_df[acc].unique())
|
| 234 |
# Convert the 'accident_quarter_bracket' column to datetime format
|
| 235 |
filled_df[acc] = pd.to_datetime(filled_df[acc], format='%Y%m').dt.strftime('%Y%m')
|
| 236 |
+
print('Unique values in', acc, 'after conversion:', filled_df[acc].unique())
|
| 237 |
+
|
| 238 |
+
|
| 239 |
# Sort the dataframe by quarter
|
| 240 |
filled_df = filled_df.sort_values(acc)
|
| 241 |
# Reset the index
|
|
|
|
| 247 |
print("No missing quarters between 2017-2022")
|
| 248 |
else:
|
| 249 |
pass#print(filtered_quarters)
|
| 250 |
+
|
| 251 |
#filled_df = filled_df[[acc, transaction] + [col for col in filled_df.columns if col not in [acc, transaction]]]
|
| 252 |
return filled_df
|
| 253 |
|