Skip to content

Commit 2149356

Browse files
committed
DEPR: pd.read_table
- pd.read_table is deprecated and replaced by pd.read_csv. - add whatsnew note - change tests to test for warning messages - change DataFrame.from_csv to use pandas.read_csv instead of pandas.read_table - Change pandas.read_clipboard to use pandas.read_csv instead of pandas.read_table
1 parent 171076c commit 2149356

File tree

11 files changed

+92
-43
lines changed

11 files changed

+92
-43
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ Deprecations
382382
- :meth:`DataFrame.to_stata`, :meth:`read_stata`, :class:`StataReader` and :class:`StataWriter` have deprecated the ``encoding`` argument. The encoding of a Stata dta file is determined by the file type and cannot be changed (:issue:`21244`).
383383
- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`)
384384
- :meth:`Series.ptp` is deprecated. Use ``numpy.ptp`` instead (:issue:`21614`)
385+
- :func:`pandas.read_table` is deprecated. Use :func:`pandas.read_csv` instead (:issue:`21948`)
385386
-
386387

387388
.. _whatsnew_0240.prior_deprecations:

pandas/core/frame.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1593,11 +1593,11 @@ def from_csv(cls, path, header=0, sep=',', index_col=0, parse_dates=True,
15931593
"for from_csv when changing your function calls",
15941594
FutureWarning, stacklevel=2)
15951595

1596-
from pandas.io.parsers import read_table
1597-
return read_table(path, header=header, sep=sep,
1598-
parse_dates=parse_dates, index_col=index_col,
1599-
encoding=encoding, tupleize_cols=tupleize_cols,
1600-
infer_datetime_format=infer_datetime_format)
1596+
from pandas.io.parsers import read_csv
1597+
return read_csv(path, header=header, sep=sep,
1598+
parse_dates=parse_dates, index_col=index_col,
1599+
encoding=encoding, tupleize_cols=tupleize_cols,
1600+
infer_datetime_format=infer_datetime_format)
16011601

16021602
def to_sparse(self, fill_value=None, kind='block'):
16031603
"""

pandas/io/clipboards.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
1111
r"""
12-
Read text from clipboard and pass to read_table. See read_table for the
12+
Read text from clipboard and pass to read_csv. See read_csv for the
1313
full argument list
1414
1515
Parameters
@@ -31,7 +31,7 @@ def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
3131
'reading from clipboard only supports utf-8 encoding')
3232

3333
from pandas.io.clipboard import clipboard_get
34-
from pandas.io.parsers import read_table
34+
from pandas.io.parsers import read_csv
3535
text = clipboard_get()
3636

3737
# try to decode (if needed on PY3)
@@ -51,7 +51,7 @@ def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
5151
# that this came from excel and set 'sep' accordingly
5252
lines = text[:10000].split('\n')[:-1][:10]
5353

54-
# Need to remove leading white space, since read_table
54+
# Need to remove leading white space, since read_csv
5555
# accepts:
5656
# a b
5757
# 0 1 2
@@ -80,7 +80,7 @@ def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
8080
if kwargs.get('engine') == 'python' and PY2:
8181
text = text.encode('utf-8')
8282

83-
return read_table(StringIO(text), sep=sep, **kwargs)
83+
return read_csv(StringIO(text), sep=sep, **kwargs)
8484

8585

8686
def to_clipboard(obj, excel=True, sep=None, **kwargs): # pragma: no cover

pandas/io/parsers.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@
326326
""" % (_parser_params % (_sep_doc.format(default="','"), _engine_doc))
327327

328328
_read_table_doc = """
329+
330+
.. deprecated:: 0.24.0
331+
Use :func:`pandas.read_csv` instead, passing `sep='\t'` if necessary.
332+
329333
Read general delimited file into DataFrame
330334
331335
%s
@@ -535,9 +539,13 @@ def _read(filepath_or_buffer, kwds):
535539
}
536540

537541

538-
def _make_parser_function(name, sep=','):
542+
def _make_parser_function(name, default_sep=','):
539543

540-
default_sep = sep
544+
# prepare read_table deprecation
545+
if name == "read_table":
546+
sep = False
547+
else:
548+
sep = default_sep
541549

542550
def parser_f(filepath_or_buffer,
543551
sep=sep,
@@ -606,11 +614,24 @@ def parser_f(filepath_or_buffer,
606614
memory_map=False,
607615
float_precision=None):
608616

617+
# deprecate read_table GH21948
618+
if name == "read_table":
619+
if sep is False and delimiter is None:
620+
warnings.warn("read_table is deprecated, use read_csv "
621+
"instead, passing sep='\\t'.",
622+
FutureWarning, stacklevel=2)
623+
else:
624+
warnings.warn("read_table is deprecated, use read_csv "
625+
"instead.",
626+
FutureWarning, stacklevel=2)
627+
if sep is False:
628+
sep = default_sep
629+
609630
# Alias sep -> delimiter.
610631
if delimiter is None:
611632
delimiter = sep
612633

613-
if delim_whitespace and delimiter is not default_sep:
634+
if delim_whitespace and delimiter != default_sep:
614635
raise ValueError("Specified a delimiter with both sep and"
615636
" delim_whitespace=True; you can only"
616637
" specify one.")
@@ -682,10 +703,10 @@ def parser_f(filepath_or_buffer,
682703
return parser_f
683704

684705

685-
read_csv = _make_parser_function('read_csv', sep=',')
706+
read_csv = _make_parser_function('read_csv', default_sep=',')
686707
read_csv = Appender(_read_csv_doc)(read_csv)
687708

688-
read_table = _make_parser_function('read_table', sep='\t')
709+
read_table = _make_parser_function('read_table', default_sep='\t')
689710
read_table = Appender(_read_table_doc)(read_table)
690711

691712

pandas/tests/io/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from pandas.io.parsers import read_table
2+
from pandas.io.parsers import read_csv
33

44

55
@pytest.fixture
@@ -17,7 +17,7 @@ def jsonl_file(datapath):
1717
@pytest.fixture
1818
def salaries_table(datapath):
1919
"""DataFrame with the salaries dataset"""
20-
return read_table(datapath('io', 'parser', 'data', 'salaries.csv'))
20+
return read_csv(datapath('io', 'parser', 'data', 'salaries.csv'), sep='\t')
2121

2222

2323
@pytest.fixture

pandas/tests/io/formats/test_format.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import numpy as np
2222
import pandas as pd
2323
from pandas import (DataFrame, Series, Index, Timestamp, MultiIndex,
24-
date_range, NaT, read_table)
24+
date_range, NaT, read_csv)
2525
from pandas.compat import (range, zip, lrange, StringIO, PY3,
2626
u, lzip, is_platform_windows,
2727
is_platform_32bit)
@@ -1225,8 +1225,8 @@ def test_to_string(self):
12251225
lines = result.split('\n')
12261226
header = lines[0].strip().split()
12271227
joined = '\n'.join(re.sub(r'\s+', ' ', x).strip() for x in lines[1:])
1228-
recons = read_table(StringIO(joined), names=header,
1229-
header=None, sep=' ')
1228+
recons = read_csv(StringIO(joined), names=header,
1229+
header=None, sep=' ')
12301230
tm.assert_series_equal(recons['B'], biggie['B'])
12311231
assert recons['A'].count() == biggie['A'].count()
12321232
assert (np.abs(recons['A'].dropna() -

pandas/tests/io/parser/test_network.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import pandas.util.testing as tm
1313
import pandas.util._test_decorators as td
1414
from pandas import DataFrame
15-
from pandas.io.parsers import read_csv, read_table
15+
from pandas.io.parsers import read_csv
1616
from pandas.compat import BytesIO, StringIO
1717

1818

@@ -44,7 +44,7 @@ def check_compressed_urls(salaries_table, compression, extension, mode,
4444
if mode != 'explicit':
4545
compression = mode
4646

47-
url_table = read_table(url, compression=compression, engine=engine)
47+
url_table = read_csv(url, sep='\t', compression=compression, engine=engine)
4848
tm.assert_frame_equal(url_table, salaries_table)
4949

5050

pandas/tests/io/parser/test_parsers.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def read_table(self, *args, **kwds):
7070
kwds = kwds.copy()
7171
kwds['engine'] = self.engine
7272
kwds['low_memory'] = self.low_memory
73-
return read_table(*args, **kwds)
73+
with tm.assert_produces_warning(FutureWarning):
74+
df = read_table(*args, **kwds)
75+
return df
7476

7577

7678
class TestCParserLowMemory(BaseParser, CParserTests):
@@ -88,7 +90,9 @@ def read_table(self, *args, **kwds):
8890
kwds = kwds.copy()
8991
kwds['engine'] = self.engine
9092
kwds['low_memory'] = True
91-
return read_table(*args, **kwds)
93+
with tm.assert_produces_warning(FutureWarning):
94+
df = read_table(*args, **kwds)
95+
return df
9296

9397

9498
class TestPythonParser(BaseParser, PythonParserTests):
@@ -103,7 +107,9 @@ def read_csv(self, *args, **kwds):
103107
def read_table(self, *args, **kwds):
104108
kwds = kwds.copy()
105109
kwds['engine'] = self.engine
106-
return read_table(*args, **kwds)
110+
with tm.assert_produces_warning(FutureWarning):
111+
df = read_table(*args, **kwds)
112+
return df
107113

108114

109115
class TestUnsortedUsecols(object):

pandas/tests/io/parser/test_unsupported.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from pandas.compat import StringIO
1616
from pandas.errors import ParserError
17-
from pandas.io.parsers import read_csv, read_table
17+
from pandas.io.parsers import read_csv
1818

1919
import pytest
2020

@@ -43,24 +43,24 @@ def test_c_engine(self):
4343

4444
# specify C engine with unsupported options (raise)
4545
with tm.assert_raises_regex(ValueError, msg):
46-
read_table(StringIO(data), engine='c',
47-
sep=None, delim_whitespace=False)
46+
read_csv(StringIO(data), engine='c',
47+
sep=None, delim_whitespace=False)
4848
with tm.assert_raises_regex(ValueError, msg):
49-
read_table(StringIO(data), engine='c', sep=r'\s')
49+
read_csv(StringIO(data), engine='c', sep=r'\s')
5050
with tm.assert_raises_regex(ValueError, msg):
51-
read_table(StringIO(data), engine='c', quotechar=chr(128))
51+
read_csv(StringIO(data), engine='c', sep='\t', quotechar=chr(128))
5252
with tm.assert_raises_regex(ValueError, msg):
53-
read_table(StringIO(data), engine='c', skipfooter=1)
53+
read_csv(StringIO(data), engine='c', skipfooter=1)
5454

5555
# specify C-unsupported options without python-unsupported options
5656
with tm.assert_produces_warning(parsers.ParserWarning):
57-
read_table(StringIO(data), sep=None, delim_whitespace=False)
57+
read_csv(StringIO(data), sep=None, delim_whitespace=False)
5858
with tm.assert_produces_warning(parsers.ParserWarning):
59-
read_table(StringIO(data), quotechar=chr(128))
59+
read_csv(StringIO(data), sep=r'\s')
6060
with tm.assert_produces_warning(parsers.ParserWarning):
61-
read_table(StringIO(data), sep=r'\s')
61+
read_csv(StringIO(data), sep='\t', quotechar=chr(128))
6262
with tm.assert_produces_warning(parsers.ParserWarning):
63-
read_table(StringIO(data), skipfooter=1)
63+
read_csv(StringIO(data), skipfooter=1)
6464

6565
text = """ A B C D E
6666
one two three four
@@ -70,9 +70,9 @@ def test_c_engine(self):
7070
msg = 'Error tokenizing data'
7171

7272
with tm.assert_raises_regex(ParserError, msg):
73-
read_table(StringIO(text), sep='\\s+')
73+
read_csv(StringIO(text), sep='\\s+')
7474
with tm.assert_raises_regex(ParserError, msg):
75-
read_table(StringIO(text), engine='c', sep='\\s+')
75+
read_csv(StringIO(text), engine='c', sep='\\s+')
7676

7777
msg = "Only length-1 thousands markers supported"
7878
data = """A|B|C

pandas/tests/io/test_common.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ def test_iterator(self):
130130

131131
@pytest.mark.parametrize('reader, module, error_class, fn_ext', [
132132
(pd.read_csv, 'os', FileNotFoundError, 'csv'),
133-
(pd.read_table, 'os', FileNotFoundError, 'csv'),
134133
(pd.read_fwf, 'os', FileNotFoundError, 'txt'),
135134
(pd.read_excel, 'xlrd', FileNotFoundError, 'xlsx'),
136135
(pd.read_feather, 'feather', Exception, 'feather'),
@@ -148,9 +147,16 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext):
148147
with pytest.raises(error_class):
149148
reader(path)
150149

150+
def test_read_non_existant_read_table(self):
151+
pytest.importorskip('os')
152+
153+
path = os.path.join(HERE, 'data', 'does_not_exist.' + 'csv')
154+
with pytest.raises(FileNotFoundError):
155+
with tm.assert_produces_warning(FutureWarning):
156+
pd.read_table(path)
157+
151158
@pytest.mark.parametrize('reader, module, path', [
152159
(pd.read_csv, 'os', ('io', 'data', 'iris.csv')),
153-
(pd.read_table, 'os', ('io', 'data', 'iris.csv')),
154160
(pd.read_fwf, 'os', ('io', 'data', 'fixed_width_format.txt')),
155161
(pd.read_excel, 'xlrd', ('io', 'data', 'test1.xlsx')),
156162
(pd.read_feather, 'feather', ('io', 'data', 'feather-0_3_1.feather')),
@@ -169,6 +175,23 @@ def test_read_fspath_all(self, reader, module, path, datapath):
169175
mypath = CustomFSPath(path)
170176
result = reader(mypath)
171177
expected = reader(path)
178+
179+
if path.endswith('.pickle'):
180+
# categorical
181+
tm.assert_categorical_equal(result, expected)
182+
else:
183+
tm.assert_frame_equal(result, expected)
184+
185+
def test_read_fspath_all_read_table(self, datapath):
186+
pytest.importorskip('os')
187+
path = datapath('io', 'data', 'iris.csv')
188+
189+
mypath = CustomFSPath(path)
190+
with tm.assert_produces_warning(FutureWarning):
191+
result = pd.read_table(mypath)
192+
with tm.assert_produces_warning(FutureWarning):
193+
expected = pd.read_table(path)
194+
172195
if path.endswith('.pickle'):
173196
# categorical
174197
tm.assert_categorical_equal(result, expected)

pandas/tests/test_multilevel.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as np
1111

1212
from pandas.core.index import Index, MultiIndex
13-
from pandas import Panel, DataFrame, Series, notna, isna, Timestamp
13+
from pandas import Panel, DataFrame, Series, notna, isna, Timestamp, read_csv
1414

1515
from pandas.core.dtypes.common import is_float_dtype, is_integer_dtype
1616
import pandas.core.common as com
@@ -512,14 +512,13 @@ def f(x):
512512
pytest.raises(com.SettingWithCopyError, f, result)
513513

514514
def test_xs_level_multiple(self):
515-
from pandas import read_table
516515
text = """ A B C D E
517516
one two three four
518517
a b 10.0032 5 -0.5109 -2.3358 -0.4645 0.05076 0.3640
519518
a q 20 4 0.4473 1.4152 0.2834 1.00661 0.1744
520519
x q 30 3 -0.6662 -0.5243 -0.3580 0.89145 2.5838"""
521520

522-
df = read_table(StringIO(text), sep=r'\s+', engine='python')
521+
df = read_csv(StringIO(text), sep=r'\s+', engine='python')
523522

524523
result = df.xs(('a', 4), level=['one', 'four'])
525524
expected = df.xs('a').xs(4, level='four')
@@ -547,14 +546,13 @@ def f(x):
547546
tm.assert_frame_equal(rs, xp)
548547

549548
def test_xs_level0(self):
550-
from pandas import read_table
551549
text = """ A B C D E
552550
one two three four
553551
a b 10.0032 5 -0.5109 -2.3358 -0.4645 0.05076 0.3640
554552
a q 20 4 0.4473 1.4152 0.2834 1.00661 0.1744
555553
x q 30 3 -0.6662 -0.5243 -0.3580 0.89145 2.5838"""
556554

557-
df = read_table(StringIO(text), sep=r'\s+', engine='python')
555+
df = read_csv(StringIO(text), sep=r'\s+', engine='python')
558556

559557
result = df.xs('a', level=0)
560558
expected = df.xs('a')

0 commit comments

Comments
 (0)