Skip to content

Adding Julian Days option (creates real columns in SQLite) #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion csvs_to_sqlite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
multiple=True,
help=("One or more custom date format strings to try when parsing dates/datetimes"),
)
@click.option(
"--date-julian",
"-dj",
multiple=True,
help=("One or more columns to store as Julian Dates (reals)"),
)
@click.option(
"--primary-key",
"-pk",
Expand Down Expand Up @@ -134,6 +140,7 @@ def cli(
date,
datetime,
datetime_format,
date_julian,
primary_key,
fts,
index,
Expand Down Expand Up @@ -174,7 +181,11 @@ def cli(
df[filename_column] = name
if shape:
shape += ",{}".format(filename_column)
sql_type_overrides = apply_shape(df, shape)
if shape:
sql_type_overrides = apply_shape(df, shape)
elif date_julian:
julian_cols = ",".join(["%s(REAL)" % s for s in date_julian])
sql_type_overrides = apply_shape(df, julian_cols, True)
apply_dates_and_datetimes(df, date, datetime, datetime_format)
dataframes.append(df)
except LoadCsvError as e:
Expand Down
13 changes: 7 additions & 6 deletions csvs_to_sqlite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,18 @@ def parse_shape(shape):
return defns


def apply_shape(df, shape):
def apply_shape(df, shape, modify=False):
# Shape is format 'county:Cty,votes:Vts(REAL)'
# Applies changes in place, returns dtype= arg for to_sql
if not shape:
return None
defns = parse_shape(shape)
# Drop any columns we don't want
cols_to_keep = [d["csv_name"] for d in defns]
cols_to_drop = [c for c in df.columns if c not in cols_to_keep]
if cols_to_drop:
df.drop(cols_to_drop, axis=1, inplace=True)
# If we are not modifying, drop any columns we don't want
if not modify:
cols_to_keep = [d["csv_name"] for d in defns]
cols_to_drop = [c for c in df.columns if c not in cols_to_keep]
if cols_to_drop:
df.drop(cols_to_drop, axis=1, inplace=True)
# Apply column renames
renames = {
d["csv_name"]: d["db_name"] for d in defns if d["csv_name"] != d["db_name"]
Expand Down