Spaces:
Sleeping
Sleeping
Commit
·
959d324
1
Parent(s):
9923f48
feat: updated website
Browse files
app.py
CHANGED
@@ -366,32 +366,48 @@ elif page == "Customer Analysis":
|
|
366 |
fecha_inicio = pd.to_datetime("2023-01-01")
|
367 |
fecha_corte = pd.to_datetime("2024-09-01")
|
368 |
|
369 |
-
# Convertir fecha_mes a datetime en
|
370 |
historical_data['fecha_mes'] = pd.to_datetime(historical_data['fecha_mes'], errors='coerce')
|
371 |
-
results['fecha_mes'] = pd.to_datetime(results['fecha_mes'], errors='coerce')
|
372 |
|
373 |
-
#
|
374 |
-
|
375 |
-
|
|
|
|
|
|
|
|
|
376 |
|
377 |
-
# Filtrar datos históricos por cliente y por el rango de fechas (2023)
|
378 |
datos_historicos = historical_data[
|
379 |
(historical_data['cliente_id'] == customer_code_str) &
|
380 |
-
(historical_data['fecha_mes'] >=
|
381 |
-
(historical_data['fecha_mes']
|
382 |
].groupby('fecha_mes')['precio_total'].sum().reset_index()
|
383 |
|
384 |
-
#
|
385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
386 |
|
387 |
-
#
|
|
|
|
|
|
|
|
|
388 |
datos_cliente_total = results.groupby('fecha_mes').agg({
|
389 |
'ventas_reales': 'sum',
|
390 |
'ventas_predichas': 'sum'
|
391 |
}).reset_index()
|
392 |
|
393 |
-
#
|
394 |
-
|
|
|
|
|
|
|
|
|
395 |
|
396 |
# Rellenar los NaN: 0 en ventas_historicas donde faltan predicciones, y viceversa
|
397 |
datos_combinados['ventas_historicas'].fillna(0, inplace=True)
|
@@ -441,7 +457,7 @@ elif page == "Customer Analysis":
|
|
441 |
xaxis_title="Fecha",
|
442 |
yaxis_title="Ventas (€)",
|
443 |
height=600,
|
444 |
-
xaxis_range=[
|
445 |
legend_title="Tipo de Ventas",
|
446 |
hovermode="x unified"
|
447 |
)
|
|
|
366 |
fecha_inicio = pd.to_datetime("2023-01-01")
|
367 |
fecha_corte = pd.to_datetime("2024-09-01")
|
368 |
|
369 |
+
# Convertir fecha_mes a datetime en el DataFrame historical_data
|
370 |
historical_data['fecha_mes'] = pd.to_datetime(historical_data['fecha_mes'], errors='coerce')
|
|
|
371 |
|
372 |
+
# Verificar los datos históricos del cliente
|
373 |
+
st.subheader("Verificar datos históricos del cliente")
|
374 |
+
st.write(historical_data[historical_data['cliente_id'] == customer_code_str].head()) # Mostrar algunos datos históricos para el cliente
|
375 |
+
|
376 |
+
# Filtrar los datos históricos por cliente y por el rango de fechas (2023)
|
377 |
+
fecha_inicio_2023 = pd.to_datetime("2023-01-01")
|
378 |
+
fecha_fin_2023 = pd.to_datetime("2023-12-31")
|
379 |
|
|
|
380 |
datos_historicos = historical_data[
|
381 |
(historical_data['cliente_id'] == customer_code_str) &
|
382 |
+
(historical_data['fecha_mes'] >= fecha_inicio_2023) &
|
383 |
+
(historical_data['fecha_mes'] <= fecha_fin_2023)
|
384 |
].groupby('fecha_mes')['precio_total'].sum().reset_index()
|
385 |
|
386 |
+
# Verificar si los datos históricos de 2023 fueron correctamente filtrados
|
387 |
+
st.subheader("Datos históricos filtrados (2023)")
|
388 |
+
st.write(datos_historicos) # Mostrar los datos históricos de 2023
|
389 |
+
|
390 |
+
# Generar un rango de fechas desde enero 2023 hasta diciembre 2023 si no hay datos
|
391 |
+
if datos_historicos.empty:
|
392 |
+
fechas_2023 = pd.date_range(start='2023-01-01', end='2023-12-31', freq='M')
|
393 |
+
datos_historicos = pd.DataFrame({'fecha_mes': fechas_2023, 'ventas_historicas': [0] * len(fechas_2023)})
|
394 |
|
395 |
+
# Renombrar la columna 'precio_total' a 'ventas_historicas' si hay datos
|
396 |
+
else:
|
397 |
+
datos_historicos.rename(columns={'precio_total': 'ventas_historicas'}, inplace=True)
|
398 |
+
|
399 |
+
# Filtrar los datos de predicciones y ventas reales para 2024
|
400 |
datos_cliente_total = results.groupby('fecha_mes').agg({
|
401 |
'ventas_reales': 'sum',
|
402 |
'ventas_predichas': 'sum'
|
403 |
}).reset_index()
|
404 |
|
405 |
+
# Generar un rango de fechas para 2024 si no hay predicciones
|
406 |
+
fechas_2024 = pd.date_range(start='2024-01-01', end='2024-12-31', freq='M')
|
407 |
+
fechas_df_2024 = pd.DataFrame({'fecha_mes': fechas_2024})
|
408 |
+
|
409 |
+
# Combinar datos históricos con predicciones y ventas reales usando un merge
|
410 |
+
datos_combinados = pd.merge(fechas_df_2024, datos_cliente_total, on='fecha_mes', how='outer').sort_values('fecha_mes')
|
411 |
|
412 |
# Rellenar los NaN: 0 en ventas_historicas donde faltan predicciones, y viceversa
|
413 |
datos_combinados['ventas_historicas'].fillna(0, inplace=True)
|
|
|
457 |
xaxis_title="Fecha",
|
458 |
yaxis_title="Ventas (€)",
|
459 |
height=600,
|
460 |
+
xaxis_range=[fecha_inicio_2023, pd.to_datetime("2024-09-30")], # Ajustar el rango del eje x a 2023-2024
|
461 |
legend_title="Tipo de Ventas",
|
462 |
hovermode="x unified"
|
463 |
)
|