Skip to content

Commit

Permalink
Pandas import update (#176)
Browse files Browse the repository at this point in the history
* Pandas import update\nNulls on nullable columns is OK

* fix show tables difference
  • Loading branch information
max-hoffman authored Apr 18, 2022
1 parent bad42e0 commit 3315207
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 4 additions & 2 deletions doltpy/cli/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def write_pandas(
"""

def writer(filepath: str):
clean = df.dropna(subset=primary_key)
clean.to_csv(filepath, index=False)
filtered = df
if import_mode != "update":
filtered = df.dropna(subset=primary_key)
filtered.to_csv(filepath, index=False)
return filepath

_import_helper(
Expand Down
4 changes: 2 additions & 2 deletions doltpy/sql/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ def get_query(table: str) -> str:

def tables(self) -> List[str]:
with self.engine.connect() as conn:
result = conn.execute("SHOW TABLES")
return [row["Table"] for row in result]
result = conn.execute("select table_name from information_schema.tables where table_schema = DATABASE();")
return [row["table_name"] for row in result]


class DoltSQLEngineContext(DoltSQLContext):
Expand Down
15 changes: 14 additions & 1 deletion tests/cli/test_write.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from doltpy.cli.write import write_pandas, CREATE
from doltpy.cli.write import write_pandas, CREATE, UPDATE
from doltpy.cli.read import read_rows
from .helpers import compare_rows
import numpy as np
import pandas as pd


Expand All @@ -19,4 +20,16 @@ def test_write_pandas(init_empty_test_repo):
compare_rows(expected, actual, "id")


def test_write_pandas_accept_nulls_on_update(init_empty_test_repo):
NULL_ROWS = [
{"name": "Anna", "adjective": "tragic", "id": "1", "date_of_death": "1877-01-01"},
{"name": "Vronksy", "adjective": "honorable", "id": "2", "date_of_death": ""},
{"name": "Oblonsky", "adjective": "buffoon", "id": "3", "date_of_death": np.NaN},
]
dolt = init_empty_test_repo
write_pandas(dolt, "characters", pd.DataFrame(NULL_ROWS[:2]), CREATE, ["id"])
write_pandas(dolt, "characters", pd.DataFrame(NULL_ROWS[2:]), UPDATE)
actual = read_rows(dolt, "characters")
expected = pd.DataFrame(NULL_ROWS).to_dict("records")
compare_rows(expected, actual, "id")

0 comments on commit 3315207

Please sign in to comment.