Spaces:
Sleeping
Sleeping
api
Browse files
app.py
CHANGED
@@ -37,8 +37,24 @@ try:
|
|
37 |
|
38 |
# Read the Excel file from the downloaded content
|
39 |
excel_content = io.BytesIO(response.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
purchase_history = pd.read_excel(excel_content, sheet_name='Transaction History',
|
41 |
-
parse_dates=['Purchase_Date'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Read Customer Profile sheet
|
44 |
excel_content.seek(0) # Reset buffer position
|
@@ -571,30 +587,19 @@ async def get_financial_analysis(customer_id: str):
|
|
571 |
|
572 |
# Get customer profile and transactions
|
573 |
customer_profile = customer_profiles[customer_profiles['Customer_Id'] == customer_id].iloc[0]
|
574 |
-
customer_transactions = purchase_history[purchase_history['Customer_Id'] == customer_id].copy()
|
575 |
|
576 |
-
#
|
577 |
if not pd.api.types.is_datetime64_any_dtype(customer_transactions['Purchase_Date']):
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
customer_transactions['Purchase_Date'] = pd.to_datetime(
|
588 |
-
customer_transactions['Purchase_Date'],
|
589 |
-
format='%Y-%m-%d'
|
590 |
-
)
|
591 |
-
except ValueError:
|
592 |
-
# If both fail, try automatic parsing
|
593 |
-
customer_transactions['Purchase_Date'] = pd.to_datetime(
|
594 |
-
customer_transactions['Purchase_Date'],
|
595 |
-
format='mixed',
|
596 |
-
dayfirst=False
|
597 |
-
)
|
598 |
|
599 |
# Calculate basic financial metrics
|
600 |
current_date = datetime.now()
|
|
|
37 |
|
38 |
# Read the Excel file from the downloaded content
|
39 |
excel_content = io.BytesIO(response.content)
|
40 |
+
def custom_date_parser(date_str):
|
41 |
+
try:
|
42 |
+
return pd.to_datetime(date_str, format='%m/%d/%Y')
|
43 |
+
except:
|
44 |
+
try:
|
45 |
+
return pd.to_datetime(date_str, format='%Y-%m-%d')
|
46 |
+
except:
|
47 |
+
return pd.to_datetime(date_str, format='mixed', dayfirst=False)
|
48 |
+
|
49 |
purchase_history = pd.read_excel(excel_content, sheet_name='Transaction History',
|
50 |
+
parse_dates=['Purchase_Date'],date_parser=custom_date_parser)
|
51 |
+
|
52 |
+
# Ensure Purchase_Date is datetime
|
53 |
+
purchase_history['Purchase_Date'] = pd.to_datetime(
|
54 |
+
purchase_history['Purchase_Date'],
|
55 |
+
format='mixed',
|
56 |
+
dayfirst=False
|
57 |
+
)
|
58 |
|
59 |
# Read Customer Profile sheet
|
60 |
excel_content.seek(0) # Reset buffer position
|
|
|
587 |
|
588 |
# Get customer profile and transactions
|
589 |
customer_profile = customer_profiles[customer_profiles['Customer_Id'] == customer_id].iloc[0]
|
590 |
+
customer_transactions = purchase_history[purchase_history['Customer_Id'] == customer_id].copy()
|
591 |
|
592 |
+
# Ensure Purchase_Date is in datetime format
|
593 |
if not pd.api.types.is_datetime64_any_dtype(customer_transactions['Purchase_Date']):
|
594 |
+
customer_transactions['Purchase_Date'] = pd.to_datetime(
|
595 |
+
customer_transactions['Purchase_Date'],
|
596 |
+
format='mixed',
|
597 |
+
dayfirst=False
|
598 |
+
)
|
599 |
+
|
600 |
+
# Print debug information
|
601 |
+
logger.info(f"Date column type: {customer_transactions['Purchase_Date'].dtype}")
|
602 |
+
logger.info(f"Sample dates: {customer_transactions['Purchase_Date'].head()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
603 |
|
604 |
# Calculate basic financial metrics
|
605 |
current_date = datetime.now()
|