INNER CODE UNIT · Python
load_csv_with_datetime
pgalko/BambooAI · web_app/app.py:401
def load_csv_with_datetime(file_path):
# Read the CSV file
df = pd.read_csv(file_path)
# Function to parse datetime and remove timezone
def parse_and_remove_tz(series):
return pd.to_datetime(series, format='%Y-%m-%d %H:%M:%S%z', utc=True).dt.tz_localize(None)
# Iterate through columns and apply vectorized parsing where appropriate
for col in df.columns:
if df[col].dtype == 'object':
# Try to parse the column as datetime
try:
df[col] = parse_and_remove_tz(df[col])
except ValueError:
# If parsing fails, leave the column as is
pass