Skip to content

sas7bdat: Check if the SAS file has zero variables #18184

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 1 commit into from
Nov 10, 2017
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/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Bug Fixes
- Bug in ``pd.read_msgpack()`` with a non existent file is passed in Python 2 (:issue:`15296`)
- Bug in ``DataFrame.groupby`` where key as tuple in a ``MultiIndex`` were interpreted as a list of keys (:issue:`17979`)
- Bug in :func:`pd.read_csv` where a ``MultiIndex`` with duplicate columns was not being mangled appropriately (:issue:`18062`)
- Bug in :func:`pd.read_sas` where a file with 0 variables gave an ``AttributeError`` incorrectly. Now it gives an ``EmptyDataError``

Conversion
^^^^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/sas/sas7bdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import pandas as pd
from pandas import compat
from pandas.io.common import get_filepath_or_buffer, BaseIterator
from pandas.errors import EmptyDataError
import numpy as np
import struct
import pandas.io.sas.sas_constants as const
Expand Down Expand Up @@ -594,6 +595,9 @@ def read(self, nrows=None):
elif nrows is None:
nrows = self.row_count

if len(self.column_types) == 0:
raise EmptyDataError("No columns to parse from file")

if self._current_row_in_file_index >= self.row_count:
return None

Expand Down
Binary file added pandas/tests/io/sas/data/zero_variables.sas7bdat
Binary file not shown.
10 changes: 10 additions & 0 deletions pandas/tests/io/sas/test_sas7bdat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import pandas as pd
from pandas.compat import PY2
import pandas.util.testing as tm
from pandas.errors import EmptyDataError
import os
import io
import numpy as np
import pytest


class TestSAS7BDAT(object):
Expand Down Expand Up @@ -174,3 +176,11 @@ def test_date_time():
df0 = pd.read_csv(fname, parse_dates=['Date1', 'Date2', 'DateTime',
'DateTimeHi', 'Taiw'])
tm.assert_frame_equal(df, df0)


def test_zero_variables():
# Check if the SAS file has zero variables (PR #18184)
dirpath = tm.get_data_path()
fname = os.path.join(dirpath, "zero_variables.sas7bdat")
with pytest.raises(EmptyDataError):
pd.read_sas(fname)