Skip to content

Commit 2e158c2

Browse files
committed
Don't track columns as separate attribute, use the CSV header when copying (columns copied can change between HDF tables for a single SQL table)
1 parent fa3526e commit 2e158c2

File tree

3 files changed

+3
-14
lines changed

3 files changed

+3
-14
lines changed

pandas_to_postgres/_base_copy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def __init__(
3333
"""
3434

3535
self.rows = 0
36-
self.columns = None
3736
self.csv_chunksize = csv_chunksize
3837

3938
if not defer_sql_objs:
@@ -112,8 +111,9 @@ def copy_from_file(self, file_object: StringIO):
112111
file_object: CSV formatted data to COPY from DataFrame to PostgreSQL
113112
"""
114113
cur = self.conn.connection.cursor()
115-
cols = ", ".join([f"{col}" for col in self.columns])
116-
sql = f"COPY {self.sql_table} ({cols}) FROM STDIN WITH CSV HEADER FREEZE"
114+
file_object.seek(0)
115+
columns = file_object.readline()
116+
sql = f"COPY {self.sql_table} ({columns}) FROM STDIN WITH CSV FREEZE"
117117
cur.copy_expert(sql=sql, file=file_object)
118118

119119
def data_formatting(self, df: DataFrame, functions: List[Callable] = [], **kwargs):

pandas_to_postgres/copy_df.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def __init__(
2020

2121
self.df = df
2222
self.levels = levels
23-
self.columns = self.df.columns
2423
self.rows = self.df.shape[0]
2524

2625
def copy(self, functions=[cast_pandas]):

pandas_to_postgres/copy_hdf.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,11 @@ def hdf_to_pg(self, data_formatters=[cast_pandas], data_formatter_kwargs={}):
6262
self.rows += len(df)
6363

6464
data_formatter_kwargs["hdf_table"] = hdf_table
65-
6665
logger.info("Formatting data")
6766
df = self.data_formatting(
6867
df, functions=data_formatters, **data_formatter_kwargs
6968
)
7069

71-
if self.columns is None:
72-
self.columns = df.columns
73-
7470
logger.info("Creating generator for chunking dataframe")
7571
for chunk in df_generator(df, self.csv_chunksize):
7672

@@ -122,9 +118,6 @@ def hdf_to_pg(self, data_formatters=[cast_pandas], data_formatter_kwargs={}):
122118
df, functions=data_formatters, **data_formatter_kwargs
123119
)
124120

125-
if self.columns is None:
126-
self.columns = df.columns
127-
128121
logger.info("Creating CSV in memory")
129122
fo = create_file_object(df)
130123

@@ -189,9 +182,6 @@ def hdf_to_pg(self, data_formatters=[cast_pandas], data_formatter_kwargs={}):
189182
df, functions=data_formatters, **data_formatter_kwargs
190183
)
191184

192-
if self.columns is None:
193-
self.columns = df.columns
194-
195185
logger.info("Creating generator for chunking dataframe")
196186
for chunk in df_generator(df, self.csv_chunksize):
197187
logger.info("Creating CSV in memory")

0 commit comments

Comments
 (0)