Skip to content

Commit

Permalink
Merge pull request #86 from treasure-data/warn-table
Browse files Browse the repository at this point in the history
Warn with invalid table argument for write_datafrme
  • Loading branch information
chezou committed Apr 23, 2020
2 parents 9dbfa37 + 2eb7d90 commit 78409e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pytd/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ def test_close(self):
pd.DataFrame([[1, 2], [3, 4]]), self.table, "error"
)

def test_table(self):
with self.assertRaises(TypeError):
self.writer.write_dataframe(pd.DataFrame([[1, 2], [3, 4]]), "foo", "error")


class BulkImportWriterTestCase(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -361,6 +365,10 @@ def test_close(self):
pd.DataFrame([[1, 2], [3, 4]]), self.table, "error"
)

def test_table(self):
with self.assertRaises(TypeError):
self.writer.write_dataframe(pd.DataFrame([[1, 2], [3, 4]]), "foo", "error")


class SparkWriterTestCase(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -442,3 +450,7 @@ def test_close(self):
self.writer.write_dataframe(
pd.DataFrame([[1, 2], [3, 4]]), self.table, "error"
)

def test_table(self):
with self.assertRaises(TypeError):
self.writer.write_dataframe(pd.DataFrame([[1, 2], [3, 4]]), "foo", "error")
15 changes: 15 additions & 0 deletions pytd/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ def write_dataframe(self, dataframe, table, if_exists):
if self.closed:
raise RuntimeError("this writer is already closed and no longer available")

if isinstance(table, str):
raise TypeError(
"table '{}' should be pytd.table.Table, not str".format(table)
)

_cast_dtypes(dataframe)

column_names, column_types = _get_schema(dataframe)
Expand Down Expand Up @@ -405,6 +410,11 @@ def write_dataframe(self, dataframe, table, if_exists, fmt="csv", keep_list=Fals
if self.closed:
raise RuntimeError("this writer is already closed and no longer available")

if isinstance(table, str):
raise TypeError(
"table '{}' should be pytd.table.Table, not str".format(table)
)

if "time" not in dataframe.columns: # need time column for bulk import
dataframe["time"] = int(time.time())

Expand Down Expand Up @@ -622,6 +632,11 @@ def write_dataframe(self, dataframe, table, if_exists):
if if_exists not in ("error", "overwrite", "append", "ignore"):
raise ValueError("invalid value for if_exists: {}".format(if_exists))

if isinstance(table, str):
raise TypeError(
"table '{}' should be pytd.table.Table, not str".format(table)
)

if self.td_spark is None:
self.td_spark = fetch_td_spark_context(
table.client.apikey,
Expand Down

0 comments on commit 78409e0

Please sign in to comment.