Skip to content

Commit f417c92

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 647f3f0 commit f417c92

File tree

11 files changed

+89
-43
lines changed

11 files changed

+89
-43
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ Deprecations
478478
- :meth:`Series.ptp` is deprecated. Use ``numpy.ptp`` instead (:issue:`21614`)
479479
- :meth:`Series.compress` is deprecated. Use ``Series[condition]`` instead (:issue:`18262`)
480480
- :meth:`Categorical.from_codes` has deprecated providing float values for the ``codes`` argument. (:issue:`21767`)
481+
- :func:`pandas.read_table` is deprecated. Use :func:`pandas.read_csv` instead (:issue:`21948`)
481482

482483
.. _whatsnew_0240.prior_deprecations:
483484

pandas/core/frame.py

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

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

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

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
@@ -331,6 +331,10 @@
331331
""" % (_parser_params % (_sep_doc.format(default="','"), _engine_doc))
332332

333333
_read_table_doc = """
334+
335+
.. deprecated:: 0.24.0
336+
Use :func:`pandas.read_csv` instead, passing `sep='\t'` if necessary.
337+
334338
Read general delimited file into DataFrame
335339
336340
%s
@@ -540,9 +544,13 @@ def _read(filepath_or_buffer, kwds):
540544
}
541545

542546

543-
def _make_parser_function(name, sep=','):
547+
def _make_parser_function(name, default_sep=','):
544548

545-
default_sep = sep
549+
# prepare read_table deprecation
550+
if name == "read_table":
551+
sep = False
552+
else:
553+
sep = default_sep
546554

547555
def parser_f(filepath_or_buffer,
548556
sep=sep,
@@ -611,11 +619,24 @@ def parser_f(filepath_or_buffer,
611619
memory_map=False,
612620
float_precision=None):
613621

622+
# deprecate read_table GH21948
623+
if name == "read_table":
624+
if sep is False and delimiter is None:
625+
warnings.warn("read_table is deprecated, use read_csv "
626+
"instead, passing sep='\\t'.",
627+
FutureWarning, stacklevel=2)
628+
else:
629+
warnings.warn("read_table is deprecated, use read_csv "
630+
"instead.",
631+
FutureWarning, stacklevel=2)
632+
if sep is False:
633+
sep = default_sep
634+
614635
# Alias sep -> delimiter.
615636
if delimiter is None:
616637
delimiter = sep
617638

618-
if delim_whitespace and delimiter is not default_sep:
639+
if delim_whitespace and delimiter != default_sep:
619640
raise ValueError("Specified a delimiter with both sep and"
620641
" delim_whitespace=True; you can only"
621642
" specify one.")
@@ -687,10 +708,10 @@ def parser_f(filepath_or_buffer,
687708
return parser_f
688709

689710

690-
read_csv = _make_parser_function('read_csv', sep=',')
711+
read_csv = _make_parser_function('read_csv', default_sep=',')
691712
read_csv = Appender(_read_csv_doc)(read_csv)
692713

693-
read_table = _make_parser_function('read_table', sep='\t')
714+
read_table = _make_parser_function('read_table', default_sep='\t')
694715
read_table = Appender(_read_table_doc)(read_table)
695716

696717

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def test_iterator(self):
131131

132132
@pytest.mark.parametrize('reader, module, error_class, fn_ext', [
133133
(pd.read_csv, 'os', FileNotFoundError, 'csv'),
134-
(pd.read_table, 'os', FileNotFoundError, 'csv'),
135134
(pd.read_fwf, 'os', FileNotFoundError, 'txt'),
136135
(pd.read_excel, 'xlrd', FileNotFoundError, 'xlsx'),
137136
(pd.read_feather, 'feather', Exception, 'feather'),
@@ -149,9 +148,14 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext):
149148
with pytest.raises(error_class):
150149
reader(path)
151150

151+
def test_read_non_existant_read_table(self):
152+
path = os.path.join(HERE, 'data', 'does_not_exist.' + 'csv')
153+
with pytest.raises(FileNotFoundError):
154+
with tm.assert_produces_warning(FutureWarning):
155+
pd.read_table(path)
156+
152157
@pytest.mark.parametrize('reader, module, path', [
153158
(pd.read_csv, 'os', ('io', 'data', 'iris.csv')),
154-
(pd.read_table, 'os', ('io', 'data', 'iris.csv')),
155159
(pd.read_fwf, 'os', ('io', 'data', 'fixed_width_format.txt')),
156160
(pd.read_excel, 'xlrd', ('io', 'data', 'test1.xlsx')),
157161
(pd.read_feather, 'feather', ('io', 'data', 'feather-0_3_1.feather')),
@@ -170,6 +174,22 @@ def test_read_fspath_all(self, reader, module, path, datapath):
170174
mypath = CustomFSPath(path)
171175
result = reader(mypath)
172176
expected = reader(path)
177+
178+
if path.endswith('.pickle'):
179+
# categorical
180+
tm.assert_categorical_equal(result, expected)
181+
else:
182+
tm.assert_frame_equal(result, expected)
183+
184+
def test_read_fspath_all_read_table(self, datapath):
185+
path = datapath('io', 'data', 'iris.csv')
186+
187+
mypath = CustomFSPath(path)
188+
with tm.assert_produces_warning(FutureWarning):
189+
result = pd.read_table(mypath)
190+
with tm.assert_produces_warning(FutureWarning):
191+
expected = pd.read_table(path)
192+
173193
if path.endswith('.pickle'):
174194
# categorical
175195
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)