Spaces:
Sleeping
Sleeping
Update appStore/prep_data.py
Browse files- appStore/prep_data.py +20 -4
appStore/prep_data.py
CHANGED
@@ -58,11 +58,27 @@ def process_giz_worldwide():
|
|
58 |
'duration.project.end': 'end_year'
|
59 |
})
|
60 |
|
61 |
-
# Convert end_year if it is stored as a numeric epoch (in ms) to "YYYY-MM-DD"
|
62 |
def convert_to_date(val):
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
giz_df['end_year'] = giz_df['end_year'].apply(convert_to_date)
|
68 |
|
|
|
58 |
'duration.project.end': 'end_year'
|
59 |
})
|
60 |
|
|
|
61 |
def convert_to_date(val):
|
62 |
+
try:
|
63 |
+
# If val is a string, first check if it represents a numeric value.
|
64 |
+
if isinstance(val, str):
|
65 |
+
val_str = val.strip()
|
66 |
+
try:
|
67 |
+
# Try converting the string to a float (i.e. it’s an epoch in string form)
|
68 |
+
num = float(val_str)
|
69 |
+
return datetime.utcfromtimestamp(num / 1000).strftime("%Y-%m-%d")
|
70 |
+
except ValueError:
|
71 |
+
# Not a numeric string; assume it's already a date string in "YYYY-MM-DD" format.
|
72 |
+
# Optionally, you can validate it:
|
73 |
+
datetime.strptime(val_str, "%Y-%m-%d")
|
74 |
+
return val_str
|
75 |
+
elif isinstance(val, (int, float)):
|
76 |
+
return datetime.utcfromtimestamp(val / 1000).strftime("%Y-%m-%d")
|
77 |
+
else:
|
78 |
+
return "Unknown"
|
79 |
+
except Exception:
|
80 |
+
return "Unknown"
|
81 |
+
|
82 |
|
83 |
giz_df['end_year'] = giz_df['end_year'].apply(convert_to_date)
|
84 |
|