Vaibhav84 commited on
Commit
a3d4e2f
·
1 Parent(s): 4f04624
Files changed (1) hide show
  1. app.py +28 -23
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() # Create a copy
575
 
576
- # Convert Purchase_Date to datetime if it's not already
577
  if not pd.api.types.is_datetime64_any_dtype(customer_transactions['Purchase_Date']):
578
- try:
579
- # First try M/D/YYYY format
580
- customer_transactions['Purchase_Date'] = pd.to_datetime(
581
- customer_transactions['Purchase_Date'],
582
- format='%m/%d/%Y'
583
- )
584
- except ValueError:
585
- try:
586
- # If that fails, try YYYY-MM-DD format
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()