Skip to content

ENH: option to disable empty string conversion to NaN in file readers #1522

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,9 +1029,9 @@ def _convert_types(values, na_values):
return values, na_count

try:
result = lib.maybe_convert_numeric(values, na_values)
result = lib.maybe_convert_numeric(values, na_values, False)
except Exception:
na_count = lib.sanitize_objects(values, na_values)
na_count = lib.sanitize_objects(values, na_values, False)
result = values

if result.dtype == np.object_:
Expand Down
26 changes: 26 additions & 0 deletions pandas/io/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ def setUp(self):
self.csv2 = os.path.join(self.dirpath, 'test2.csv')
self.xls1 = os.path.join(self.dirpath, 'test.xls')

def test_empty_string(self):
data = """\
One,Two,Three
a,1,one
b,2,two
,3,three
d,4,nan
e,5,five
nan,6,
g,7,seven
"""
df = read_csv(StringIO(data))
xp = DataFrame({'One' : ['a', 'b', np.nan, 'd', 'e', np.nan, 'g'],
'Two' : [1,2,3,4,5,6,7],
'Three' : ['one', 'two', 'three', np.nan, 'five',
np.nan, 'seven']})
assert_frame_equal(xp.reindex(columns=df.columns), df)

df = read_csv(StringIO(data), na_values={'One': [], 'Three': []})
xp = DataFrame({'One' : ['a', 'b', '', 'd', 'e', 'nan', 'g'],
'Two' : [1,2,3,4,5,6,7],
'Three' : ['one', 'two', 'three', 'nan', 'five',
'', 'seven']})
assert_frame_equal(xp.reindex(columns=df.columns), df)


def test_read_csv(self):
pass

Expand Down
15 changes: 10 additions & 5 deletions pandas/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ def is_date_array(ndarray[object] values):
return True


def maybe_convert_numeric(ndarray[object] values, set na_values):
def maybe_convert_numeric(ndarray[object] values, set na_values,
convert_empty=True):
'''
Type inference function-- convert strings to numeric (potentially) and
convert to proper dtype array
Expand Down Expand Up @@ -275,8 +276,11 @@ def maybe_convert_numeric(ndarray[object] values, set na_values):
floats[i] = complexes[i] = nan
seen_float = 1
elif len(val) == 0:
floats[i] = complexes[i] = nan
seen_float = 1
if convert_empty:
floats[i] = complexes[i] = nan
seen_float = 1
else:
raise ValueError('Empty string encountered')
elif util.is_complex_object(val):
complexes[i] = val
seen_complex = 1
Expand Down Expand Up @@ -573,7 +577,8 @@ def try_parse_datetime_components(ndarray[object] years, ndarray[object] months,

return result

def sanitize_objects(ndarray[object] values, set na_values):
def sanitize_objects(ndarray[object] values, set na_values,
convert_empty=True):
cdef:
Py_ssize_t i, n
object val, onan
Expand All @@ -585,7 +590,7 @@ def sanitize_objects(ndarray[object] values, set na_values):

for i from 0 <= i < n:
val = values[i]
if val == '' or val in na_values:
if (convert_empty and val == '') or (val in na_values):
values[i] = onan
na_count += 1
elif val in memo:
Expand Down