Spaces:
Sleeping
Sleeping
Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Utility functions for data conversion
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
|
7 |
+
def safe_convert_numeric(series):
|
8 |
+
"""
|
9 |
+
Safely convert a pandas Series to numeric values, coercing errors to NaN.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
series: Pandas Series to convert
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
Pandas Series with numeric values
|
16 |
+
"""
|
17 |
+
try:
|
18 |
+
return pd.to_numeric(series, errors='coerce')
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Error converting to numeric: {e}")
|
21 |
+
return series
|
22 |
+
|
23 |
+
|
24 |
+
def safe_convert_datetime(series):
|
25 |
+
"""
|
26 |
+
Safely convert a pandas Series to datetime, coercing errors to NaN.
|
27 |
+
|
28 |
+
Args:
|
29 |
+
series: Pandas Series to convert
|
30 |
+
|
31 |
+
Returns:
|
32 |
+
Pandas Series with datetime values
|
33 |
+
"""
|
34 |
+
try:
|
35 |
+
return pd.to_datetime(series, errors='coerce')
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Error converting to datetime: {e}")
|
38 |
+
return series
|