I’m trying to import a local csv file using uplink. I have no problems with text and number types. But I can’t figure how to import date columns.
What I have is:
A datatable named “datetable” with one column named “datetime” of type date.
A csv with one column and one row:
This local python code (the same as the one in the docs). It’s working for other types.
What I have tried for a couple of hours, and not worked:
Try to match the date format in two ways (yyyy-mm-dd ; dd-mm-yyyy)
I try with date(yyyy,mm,dd) instead of yyy-mm-dd.
Finally I exported the datatable and imported it again (after removing the ID column). I thougth that in this case the types will match exactly the date format of Anvil. But still nothing.
I’ve always get the same error:
anvil.tables._errors.TableError: “Column ‘datetime’ is a date - cannot set it to a string”
You have to manually convert the datetime column in your csv to a datetime object before adding the row to the table. The table does not automatically convert strings to datetimes.
def import_csv_data(file):
with open(file,"r") as r:
df = pd.read_csv(f)
for d in df.to_dict(orient="records"):
d['datetime'] = datetime.strptime(d['datetime'],"%Y-%m-%d")
app_tables.datatable.add_row(**d)
This isn’t test, off the fly so you might need to run on a single row and debug a bit.