Skip to content

Commit

Permalink
DEPR: pd.read_table
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
dahlbaek committed Jul 18, 2018
1 parent 537b65c commit 9eb7631
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 105 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Expand Up @@ -319,6 +319,7 @@ Deprecations
- :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`).
- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`)
- :meth:`Series.ptp` is deprecated. Use ``numpy.ptp`` instead (:issue:`21614`)
- :func:`pandas.read_table` is deprecated. Use ``pandas.read_csv`` instead (:issue:`21948`)
-

.. _whatsnew_0240.prior_deprecations:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Expand Up @@ -1592,8 +1592,8 @@ def from_csv(cls, path, header=0, sep=',', index_col=0, parse_dates=True,
"for from_csv when changing your function calls",
FutureWarning, stacklevel=2)

from pandas.io.parsers import read_table
return read_table(path, header=header, sep=sep,
from pandas.io.parsers import read_csv
return read_csv(path, header=header, sep=sep,
parse_dates=parse_dates, index_col=index_col,
encoding=encoding, tupleize_cols=tupleize_cols,
infer_datetime_format=infer_datetime_format)
Expand Down
8 changes: 4 additions & 4 deletions pandas/io/clipboards.py
Expand Up @@ -9,7 +9,7 @@

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

from pandas.io.clipboard import clipboard_get
from pandas.io.parsers import read_table
from pandas.io.parsers import read_csv
text = clipboard_get()

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

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

return read_table(StringIO(text), sep=sep, **kwargs)
return read_csv(StringIO(text), sep=sep, **kwargs)


def to_clipboard(obj, excel=True, sep=None, **kwargs): # pragma: no cover
Expand Down
21 changes: 21 additions & 0 deletions pandas/io/parsers.py
Expand Up @@ -326,6 +326,10 @@
""" % (_parser_params % (_sep_doc.format(default="','"), _engine_doc))

_read_table_doc = """
.. deprecated:: 0.24.0
Use :func:`pandas.read_csv` instead, passing `sep='\t'` if necessary.
Read general delimited file into DataFrame
%s
Expand Down Expand Up @@ -539,6 +543,10 @@ def _make_parser_function(name, sep=','):

default_sep = sep

# prepare read_table deprecation
if name == "read_table":
sep = False

def parser_f(filepath_or_buffer,
sep=sep,
delimiter=None,
Expand Down Expand Up @@ -606,6 +614,19 @@ def parser_f(filepath_or_buffer,
memory_map=False,
float_precision=None):

# deprecate read_table
if name == "read_table":
if sep is False and delimiter is None:
warnings.warn("read_table is deprecated, use read_csv "
"with sep='\\t' instead.",
FutureWarning, stacklevel=2)
else:
warnings.warn("read_table is deprecated, use read_csv "
"instead.",
FutureWarning, stacklevel=2)
if sep is False:
sep = '\t'

# Alias sep -> delimiter.
if delimiter is None:
delimiter = sep
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/io/conftest.py
@@ -1,5 +1,6 @@
import pytest
from pandas.io.parsers import read_table
import pandas.util.testing as tm


@pytest.fixture
Expand All @@ -17,7 +18,10 @@ def jsonl_file(datapath):
@pytest.fixture
def salaries_table(datapath):
"""DataFrame with the salaries dataset"""
return read_table(datapath('io', 'parser', 'data', 'salaries.csv'))
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
df = read_table(datapath('io', 'parser', 'data', 'salaries.csv'))
return df


@pytest.fixture
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/io/formats/test_format.py
Expand Up @@ -1225,8 +1225,10 @@ def test_to_string(self):
lines = result.split('\n')
header = lines[0].strip().split()
joined = '\n'.join(re.sub(r'\s+', ' ', x).strip() for x in lines[1:])
recons = read_table(StringIO(joined), names=header,
header=None, sep=' ')
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
recons = read_table(StringIO(joined), names=header,
header=None, sep=' ')
tm.assert_series_equal(recons['B'], biggie['B'])
assert recons['A'].count() == biggie['A'].count()
assert (np.abs(recons['A'].dropna() -
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/io/parser/c_parser_only.py
Expand Up @@ -34,7 +34,9 @@ def test_buffer_overflow(self, malf):
# buffer overflows in tokenizer.c
cperr = 'Buffer overflow caught - possible malformed input file.'
with pytest.raises(pd.errors.ParserError) as excinfo:
self.read_table(StringIO(malf))
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
self.read_table(StringIO(malf))
assert cperr in str(excinfo.value)

def test_buffer_rd_bytes(self):
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/io/parser/comment.py
Expand Up @@ -24,8 +24,10 @@ def test_comment(self):
df = self.read_csv(StringIO(data), comment='#')
tm.assert_numpy_array_equal(df.values, expected)

df = self.read_table(StringIO(data), sep=',', comment='#',
na_values=['NaN'])
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
df = self.read_table(StringIO(data), sep=',', comment='#',
na_values=['NaN'])
tm.assert_numpy_array_equal(df.values, expected)

def test_line_comment(self):
Expand Down

0 comments on commit 9eb7631

Please sign in to comment.