Skip to content

Commit b594137

Browse files
gfyoungjreback
authored andcommitted
DEPR: Deprecate read_csv arguments fully (#17865)
Issue warnings on `read_csv` deprecated args in full, even if the normal defaults were passed in. Closes gh-17828.
1 parent 446d5b4 commit b594137

File tree

4 files changed

+51
-32
lines changed

4 files changed

+51
-32
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ def _from_arrays(cls, arrays, columns, index, dtype=None):
12941294

12951295
@classmethod
12961296
def from_csv(cls, path, header=0, sep=',', index_col=0, parse_dates=True,
1297-
encoding=None, tupleize_cols=False,
1297+
encoding=None, tupleize_cols=None,
12981298
infer_datetime_format=False):
12991299
"""
13001300
Read CSV file (DEPRECATED, please use :func:`pandas.read_csv`

pandas/io/parsers.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ def _read(filepath_or_buffer, kwds):
529529
'buffer_lines',
530530
'float_precision',
531531
}
532+
533+
_deprecated_defaults = {
534+
'as_recarray': None,
535+
'buffer_lines': None,
536+
'compact_ints': None,
537+
'use_unsigned': None,
538+
'tupleize_cols': None
539+
}
532540
_deprecated_args = {
533541
'as_recarray',
534542
'buffer_lines',
@@ -594,7 +602,7 @@ def parser_f(filepath_or_buffer,
594602
comment=None,
595603
encoding=None,
596604
dialect=None,
597-
tupleize_cols=False,
605+
tupleize_cols=None,
598606

599607
# Error Handling
600608
error_bad_lines=True,
@@ -606,9 +614,9 @@ def parser_f(filepath_or_buffer,
606614
# Internal
607615
doublequote=True,
608616
delim_whitespace=False,
609-
as_recarray=False,
610-
compact_ints=False,
611-
use_unsigned=False,
617+
as_recarray=None,
618+
compact_ints=None,
619+
use_unsigned=None,
612620
low_memory=_c_parser_defaults['low_memory'],
613621
buffer_lines=None,
614622
memory_map=False,
@@ -831,12 +839,14 @@ def _get_options_with_defaults(self, engine):
831839
if ('python' in engine and
832840
argname not in _python_unsupported):
833841
pass
842+
elif value == _deprecated_defaults.get(argname, default):
843+
pass
834844
else:
835845
raise ValueError(
836846
'The %r option is not supported with the'
837847
' %r engine' % (argname, engine))
838848
else:
839-
value = default
849+
value = _deprecated_defaults.get(argname, default)
840850
options[argname] = value
841851

842852
if engine == 'python-fwf':
@@ -962,6 +972,8 @@ def _clean_options(self, options, engine):
962972

963973
for arg in _deprecated_args:
964974
parser_default = _c_parser_defaults[arg]
975+
depr_default = _deprecated_defaults[arg]
976+
965977
msg = ("The '{arg}' argument has been deprecated "
966978
"and will be removed in a future version."
967979
.format(arg=arg))
@@ -970,10 +982,13 @@ def _clean_options(self, options, engine):
970982
msg += ' Please call pd.to_csv(...).to_records() instead.'
971983
elif arg == 'tupleize_cols':
972984
msg += (' Column tuples will then '
973-
'always be converted to MultiIndex')
985+
'always be converted to MultiIndex.')
974986

975-
if result.get(arg, parser_default) != parser_default:
987+
if result.get(arg, depr_default) != depr_default:
988+
# raise Exception(result.get(arg, depr_default), depr_default)
976989
depr_warning += msg + '\n\n'
990+
else:
991+
result[arg] = parser_default
977992

978993
if depr_warning != '':
979994
warnings.warn(depr_warning, FutureWarning, stacklevel=2)

pandas/tests/frame/test_to_csv.py

+23-24
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ def _do_test(df, r_dtype=None, c_dtype=None,
267267

268268
with ensure_clean('__tmp_to_csv_moar__') as path:
269269
df.to_csv(path, encoding='utf8',
270-
chunksize=chunksize, tupleize_cols=False)
271-
recons = self.read_csv(path, tupleize_cols=False, **kwargs)
270+
chunksize=chunksize)
271+
recons = self.read_csv(path, **kwargs)
272272
else:
273273
kwargs['header'] = 0
274274

@@ -542,35 +542,35 @@ def _make_frame(names=None):
542542

543543
# column & index are multi-index
544544
df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4)
545-
df.to_csv(path, tupleize_cols=False)
546-
result = read_csv(path, header=[0, 1, 2, 3], index_col=[
547-
0, 1], tupleize_cols=False)
545+
df.to_csv(path)
546+
result = read_csv(path, header=[0, 1, 2, 3],
547+
index_col=[0, 1])
548548
assert_frame_equal(df, result)
549549

550550
# column is mi
551551
df = mkdf(5, 3, r_idx_nlevels=1, c_idx_nlevels=4)
552-
df.to_csv(path, tupleize_cols=False)
552+
df.to_csv(path)
553553
result = read_csv(
554-
path, header=[0, 1, 2, 3], index_col=0, tupleize_cols=False)
554+
path, header=[0, 1, 2, 3], index_col=0)
555555
assert_frame_equal(df, result)
556556

557557
# dup column names?
558558
df = mkdf(5, 3, r_idx_nlevels=3, c_idx_nlevels=4)
559-
df.to_csv(path, tupleize_cols=False)
560-
result = read_csv(path, header=[0, 1, 2, 3], index_col=[
561-
0, 1, 2], tupleize_cols=False)
559+
df.to_csv(path)
560+
result = read_csv(path, header=[0, 1, 2, 3],
561+
index_col=[0, 1, 2])
562562
assert_frame_equal(df, result)
563563

564564
# writing with no index
565565
df = _make_frame()
566-
df.to_csv(path, tupleize_cols=False, index=False)
567-
result = read_csv(path, header=[0, 1], tupleize_cols=False)
566+
df.to_csv(path, index=False)
567+
result = read_csv(path, header=[0, 1])
568568
assert_frame_equal(df, result)
569569

570570
# we lose the names here
571571
df = _make_frame(True)
572-
df.to_csv(path, tupleize_cols=False, index=False)
573-
result = read_csv(path, header=[0, 1], tupleize_cols=False)
572+
df.to_csv(path, index=False)
573+
result = read_csv(path, header=[0, 1])
574574
assert _all_none(*result.columns.names)
575575
result.columns.names = df.columns.names
576576
assert_frame_equal(df, result)
@@ -589,15 +589,15 @@ def _make_frame(names=None):
589589

590590
# whatsnew example
591591
df = _make_frame()
592-
df.to_csv(path, tupleize_cols=False)
593-
result = read_csv(path, header=[0, 1], index_col=[
594-
0], tupleize_cols=False)
592+
df.to_csv(path)
593+
result = read_csv(path, header=[0, 1],
594+
index_col=[0])
595595
assert_frame_equal(df, result)
596596

597597
df = _make_frame(True)
598-
df.to_csv(path, tupleize_cols=False)
599-
result = read_csv(path, header=[0, 1], index_col=[
600-
0], tupleize_cols=False)
598+
df.to_csv(path)
599+
result = read_csv(path, header=[0, 1],
600+
index_col=[0])
601601
assert_frame_equal(df, result)
602602

603603
# column & index are multi-index (compatibility)
@@ -613,18 +613,17 @@ def _make_frame(names=None):
613613

614614
# invalid options
615615
df = _make_frame(True)
616-
df.to_csv(path, tupleize_cols=False)
616+
df.to_csv(path)
617617

618618
for i in [6, 7]:
619619
msg = 'len of {i}, but only 5 lines in file'.format(i=i)
620620
with tm.assert_raises_regex(ParserError, msg):
621-
read_csv(path, tupleize_cols=False,
622-
header=lrange(i), index_col=0)
621+
read_csv(path, header=lrange(i), index_col=0)
623622

624623
# write with cols
625624
with tm.assert_raises_regex(TypeError, 'cannot specify cols '
626625
'with a MultiIndex'):
627-
df.to_csv(path, tupleize_cols=False, columns=['foo', 'bar'])
626+
df.to_csv(path, columns=['foo', 'bar'])
628627

629628
with ensure_clean('__tmp_to_csv_multiindex__') as path:
630629
# empty

pandas/tests/io/parser/test_unsupported.py

+5
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,15 @@ class TestDeprecatedFeatures(object):
129129

130130
@pytest.mark.parametrize("engine", ["c", "python"])
131131
@pytest.mark.parametrize("kwargs", [{"as_recarray": True},
132+
{"as_recarray": False},
132133
{"buffer_lines": True},
134+
{"buffer_lines": False},
133135
{"compact_ints": True},
136+
{"compact_ints": False},
134137
{"use_unsigned": True},
138+
{"use_unsigned": False},
135139
{"tupleize_cols": True},
140+
{"tupleize_cols": False},
136141
{"skip_footer": 1}])
137142
def test_deprecated_args(self, engine, kwargs):
138143
data = "1,2,3"

0 commit comments

Comments
 (0)