Skip to content

BUG: AttributeError when read_sas used with GCS #33070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ I/O
- Bug in :meth:`read_csv` was causing a segfault when there were blank lines between the header and data rows (:issue:`28071`)
- Bug in :meth:`read_csv` was raising a misleading exception on a permissions issue (:issue:`23784`)
- Bug in :meth:`read_csv` was raising an ``IndexError`` when header=None and 2 extra data columns
- Bug in :meth:`read_sas` was raising an ``AttributeError`` when reading files from Google Cloud Storage (issue:`33069`)
- Bug in :meth:`DataFrame.to_sql` where an ``AttributeError`` was raised when saving an out of bounds date (:issue:`26761`)

Plotting
Expand Down
11 changes: 3 additions & 8 deletions pandas/io/sas/sas_xport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""
from collections import abc
from datetime import datetime
from io import BytesIO
import struct
import warnings

Expand Down Expand Up @@ -263,13 +262,9 @@ def __init__(
if isinstance(filepath_or_buffer, (str, bytes)):
self.filepath_or_buffer = open(filepath_or_buffer, "rb")
else:
# Copy to BytesIO, and ensure no encoding
contents = filepath_or_buffer.read()
try:
contents = contents.encode(self._encoding)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the encoding every applied to contents after this change? Does this matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The encoding was only to get from text-mode to binary mode. As far as I can tell, SAS XPORT files don't have a text-mode, so the encoding is unnecessary in Python 3.

Later on, bytes corresponding to text columns are decoded into strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right.

except UnicodeEncodeError:
pass
self.filepath_or_buffer = BytesIO(contents)
# Since xport files include non-text byte sequences, xport files
# should already be opened in binary mode in Python 3.
self.filepath_or_buffer = filepath_or_buffer

self._read_header()

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/sas/test_xport.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def setup_method(self, datapath):
self.dirpath = datapath("io", "sas", "data")
self.file01 = os.path.join(self.dirpath, "DEMO_G.xpt")
self.file02 = os.path.join(self.dirpath, "SSHSV1_A.xpt")
self.file02b = open(os.path.join(self.dirpath, "SSHSV1_A.xpt"), "rb")
self.file03 = os.path.join(self.dirpath, "DRXFCD_G.xpt")
self.file04 = os.path.join(self.dirpath, "paxraw_d_short.xpt")

Expand Down Expand Up @@ -119,6 +120,16 @@ def test2(self):
data = read_sas(self.file02)
tm.assert_frame_equal(data, data_csv)

def test2_binary(self):
# Test with SSHSV1_A.xpt, read as a binary file

# Compare to this
data_csv = pd.read_csv(self.file02.replace(".xpt", ".csv"))
numeric_as_float(data_csv)

data = read_sas(self.file02b, format="xport")
tm.assert_frame_equal(data, data_csv)

def test_multiple_types(self):
# Test with DRXFCD_G.xpt (contains text and numeric variables)

Expand Down