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 2 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 @@ -385,6 +385,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`)


Plotting
Expand Down
4 changes: 0 additions & 4 deletions pandas/io/sas/sas_xport.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,6 @@ def __init__(
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to determine if it is a binary file handle already, so that one can avoid copying the file to a BytesIO and the having to reread this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the conversion to BytesIO is always unnecessary on Python 3. Removed this conversion and added a comment with my thoughts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per https://www.loc.gov/preservation/digital/formats/fdd/fdd000464.shtml, rows are padded with Null bytes, so I think reading an XPORT file in text-mode on Python 3 will always fail.


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