Skip to content

Commit

Permalink
Revert "SNOW-641540 Fix dtypes of fetch_pandas_all for empty result" (#…
Browse files Browse the repository at this point in the history
…1237)

This reverts commit 128dd75.
  • Loading branch information
sfc-gh-mkeller authored Aug 24, 2022
1 parent 128dd75 commit e145e62
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/snowflake/connector/result_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def _get_arrow_iter(
return self._create_iter(iter_unit=IterUnit.TABLE_UNIT, connection=connection)

def _create_empty_table(self) -> Table:
"""Returns empty Arrow table based on schema"""
"""Returns emtpy Arrow table based on schema"""
if installed_pandas:
# initialize pyarrow type array corresponding to FIELD_TYPES
FIELD_TYPE_TO_PA_TYPE = [e.pa_type() for e in FIELD_TYPES]
Expand Down
3 changes: 1 addition & 2 deletions src/snowflake/connector/result_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ def _fetch_pandas_all(self, **kwargs) -> pandas.DataFrame:
ignore_index=True, # Don't keep in result batch indexes
**kwargs,
)
# Empty dataframe
return self.batches[0].to_pandas()
return pandas.DataFrame(columns=self.batches[0].column_names)

def _get_metrics(self) -> dict[str, int]:
"""Sum up all the chunks' metrics and show them together."""
Expand Down
26 changes: 10 additions & 16 deletions test/integ/pandas/test_arrow_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,18 +1169,6 @@ def assert_dtype_equal(a, b):
)


def assert_pandas_batch_types(
batch: pandas.DataFrame, expected_types: list[type]
) -> None:
assert batch.dtypes is not None

pandas_dtypes = batch.dtypes
# pd.string is represented as an np.object
# np.dtype string is not the same as pd.string (python)
for pandas_dtype, expected_type in zip(pandas_dtypes, expected_types):
assert_dtype_equal(pandas_dtype.type, numpy.dtype(expected_type).type)


def test_pandas_dtypes(conn_cnx):
with conn_cnx(
session_parameters={
Expand All @@ -1191,12 +1179,18 @@ def test_pandas_dtypes(conn_cnx):
cur.execute(
"select 1::integer, 2.3::double, 'foo'::string, current_timestamp()::timestamp where 1=0"
)
expected_types = [numpy.int64, float, object, numpy.datetime64]
assert_pandas_batch_types(cur.fetch_pandas_all(), expected_types)

batches = cur.get_result_batches()
batch = batches[0].to_pandas()

assert batch.dtypes is not None
assert batches[0].to_arrow() is not True
assert_pandas_batch_types(batches[0].to_pandas(), expected_types)

pandas_dtypes = batch.dtypes
expected_types = [numpy.int64, float, object, numpy.datetime64]
# pd.string is represented as an np.object
# np.dtype string is not the same as pd.string (python)
for i, typ in enumerate(expected_types):
assert_dtype_equal(pandas_dtypes[i].type, numpy.dtype(typ).type)


def test_timestamp_tz(conn_cnx):
Expand Down

0 comments on commit e145e62

Please sign in to comment.