1010CHUNKSIZE = 10 ** 6
1111CONNECTION_STRING = "sqlite:///{}" .format (DATABASE_NAME )
1212
13- # Column dtypes for tables that trigger pandas mixed-type warnings when
14- # loaded with the default low_memory chunked inference (see #1237).
15- # Types mirror mimic-iii/buildmimic/postgres/postgres_create_tables.sql.
16- TABLE_DTYPES = {
17- "chartevents" : {
18- "WARNING" : "Int64" ,
19- "ERROR" : "Int64" ,
20- "RESULTSTATUS" : "string" ,
21- "STOPPED" : "string" ,
22- },
23- "datetimeevents" : {
24- "WARNING" : "Int64" ,
25- "ERROR" : "Int64" ,
26- "RESULTSTATUS" : "string" ,
27- "STOPPED" : "string" ,
28- },
29- "inputevents_cv" : {
30- "ORIGINALRATE" : "float64" ,
31- "ORIGINALRATEUOM" : "string" ,
32- "ORIGINALSITE" : "string" ,
33- },
34- "noteevents" : {
35- "CHARTTIME" : "string" ,
36- "STORETIME" : "string" ,
37- "ISERROR" : "string" ,
38- },
39- }
40-
4113
4214def _table_name_from_csv (filename : str ) -> str :
4315 """Derive SQL table name from a CSV path (literal suffix, not str.strip)."""
@@ -49,18 +21,6 @@ def _table_name_from_csv(filename: str) -> str:
4921 return name .lower ()
5022
5123
52- def _read_csv (path , table , ** kwargs ):
53- # low_memory=False avoids dtype re-inference across chunks; table-specific
54- # dtypes pin the columns that otherwise flip between numeric/object.
55- return pd .read_csv (
56- path ,
57- index_col = "ROW_ID" ,
58- low_memory = False ,
59- dtype = TABLE_DTYPES .get (table ),
60- ** kwargs ,
61- )
62-
63-
6424if os .path .exists (DATABASE_NAME ):
6525 msg = "File {} already exists." .format (DATABASE_NAME )
6626 print (msg )
@@ -77,11 +37,11 @@ def _read_csv(path, table, **kwargs):
7737for table , f in sorted (files_by_table .items ()):
7838 print ("Starting processing {}" .format (f ))
7939 if os .path .getsize (f ) < THRESHOLD_SIZE :
80- df = _read_csv (f , table )
40+ df = pd . read_csv (f , index_col = "ROW_ID" )
8141 df .to_sql (table , CONNECTION_STRING )
8242 else :
8343 # If the file is too large, let's do the work in chunks
84- for chunk in _read_csv (f , table , chunksize = CHUNKSIZE ):
44+ for chunk in pd . read_csv (f , index_col = "ROW_ID" , chunksize = CHUNKSIZE ):
8545 chunk .to_sql (table , CONNECTION_STRING , if_exists = "append" )
8646 print ("Finished processing {}" .format (f ))
8747
0 commit comments