Skip to content

Instantly share code, notes, and snippets.

View martimors's full-sized avatar
🤳

Martin Morset martimors

🤳
  • Copenhagen
View GitHub Profile
@martimors
martimors / date_to_string.py
Created January 16, 2020 12:40
Convert all dates to string in pandas dataframe
def datetime_to_string(df):
"""
Convert all datetimes to strings in a dataframe.
Assumes dates are all timezone aware.
"""
datetime_columns = df.select_dtypes(include="datetimetz").columns.to_list()
for col in datetime_columns:
df[col] = df[col].dt.strftime(date_format="%Y-%m-%dT%H:%M:%S%z")
return df
@martimors
martimors / parse_datetime.py
Last active January 16, 2020 09:49
Parse a datetime string in Python
from datetime import datetime
class DateTimeParseError(Exception):
pass
def parse_datetime(date):
"""
Checks if a date has a valid format ISO8601 formatting and returns
a parsed datetime object with the correct timezone if applicable.