Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5c4a388

Browse filesBrowse files
committed
Remove freeze
1 parent b3c9b82 commit 5c4a388
Copy full SHA for 5c4a388

File tree

Expand file treeCollapse file tree

1 file changed

+17
-2
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+17
-2
lines changed

‎pandas_to_postgres/append_df.py

Copy file name to clipboardExpand all lines: pandas_to_postgres/append_df.py
+17-2Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DataFrameCopyAppend(BaseCopy):
1212
"""
1313

1414
def __init__(
15-
self, df, defer_sql_objs=False, conn=None, table_obj=None, csv_chunksize=10 ** 6
15+
self, df, defer_sql_objs=False, conn=None, table_obj=None, csv_chunksize=10 ** 6
1616
):
1717
"""
1818
Parameters
@@ -49,6 +49,22 @@ def drop_fks(self):
4949
def drop_pk(self):
5050
pass
5151

52+
def copy_from_file(self, file_object):
53+
"""
54+
COPY to PostgreSQL table using StringIO CSV object
55+
Parameters
56+
----------
57+
file_object: StringIO
58+
CSV formatted data to COPY from DataFrame to PostgreSQL
59+
"""
60+
cur = self.conn.connection.cursor()
61+
file_object.seek(0)
62+
columns = file_object.readline()
63+
sql = "COPY {table} ({columns}) FROM STDIN WITH CSV".format(
64+
table=self.sql_table, columns=columns
65+
)
66+
cur.copy_expert(sql=sql, file=file_object)
67+
5268
def copy(self, functions=[cast_pandas]):
5369
self.drop_fks()
5470
self.drop_pk()
@@ -58,7 +74,6 @@ def copy(self, functions=[cast_pandas]):
5874

5975
self.logger.info("Creating generator for chunking dataframe")
6076
for chunk in df_generator(self.df, self.csv_chunksize):
61-
6277
self.logger.info("Creating CSV in memory")
6378
fo = create_file_object(chunk)
6479

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.