Skip to content

Instantly share code, notes, and snippets.

@martimors
Created January 16, 2020 12:40
Show Gist options
  • Save martimors/8e3dd2dc782a7cb187cc80953a5ea616 to your computer and use it in GitHub Desktop.
Save martimors/8e3dd2dc782a7cb187cc80953a5ea616 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment