Created
June 6, 2020 21:29
-
-
Save bertrand-caron/d7022e2ca2545a0a3a948764afdf7456 to your computer and use it in GitHub Desktop.
Script for reformatting Meetup.com exports into Excel friendly formats (requires first converting from Excel to CSV).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from datetime import datetime | |
from argparse import ArgumentParser, Namespace | |
DATETIME_COLUMNS = [ | |
"Joined Group on", | |
"Last visited group on", | |
"Last Attended", | |
] | |
DATETIME_INPUT_FORMAT = "%B %d, %Y" | |
def parse_args() -> Namespace: | |
parser = ArgumentParser(description='Reformat the dates in a CSV file') | |
parser.add_argument("--datetime-input-format", help='', default=DATETIME_INPUT_FORMAT, type=str) | |
parser.add_argument("csv_input_file", help='Path to CSV file', type=str) | |
parser.add_argument("--datetime-columns", help='List of column names (headers) to reformat', nargs="*", type=str, default=DATETIME_COLUMNS) | |
return parser.parse_args() | |
if __name__ == "__main__": | |
args = parse_args() | |
df = pd.read_csv(args.csv_input_file) | |
def reformat_date(s: str) -> str: | |
if pd.isnull(s): | |
return s | |
else: | |
return datetime.strptime(s, args.datetime_input_format) | |
for column in args.datetime_columns: | |
df[column] = df[column].map(reformat_date) | |
df.to_csv(args.csv_input_file.replace('.csv', '_reformatted.csv')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment