Skip to content

Deprecate prefix argument in read_csv and read_table #44713

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

Merged
merged 4 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ Other Deprecations
- Deprecated casting behavior when setting timezone-aware value(s) into a timezone-aware :class:`Series` or :class:`DataFrame` column when the timezones do not match. Previously this cast to object dtype. In a future version, the values being inserted will be converted to the series or column's existing timezone (:issue:`37605`)
- Deprecated casting behavior when passing an item with mismatched-timezone to :meth:`DatetimeIndex.insert`, :meth:`DatetimeIndex.putmask`, :meth:`DatetimeIndex.where` :meth:`DatetimeIndex.fillna`, :meth:`Series.mask`, :meth:`Series.where`, :meth:`Series.fillna`, :meth:`Series.shift`, :meth:`Series.replace`, :meth:`Series.reindex` (and :class:`DataFrame` column analogues). In the past this has cast to object dtype. In a future version, these will cast the passed item to the index or series's timezone (:issue:`37605`)
- Deprecated the 'errors' keyword argument in :meth:`Series.where`, :meth:`DataFrame.where`, :meth:`Series.mask`, and meth:`DataFrame.mask`; in a future version the argument will be removed (:issue:`44294`)
- Deprecated the ``header`` keyword argument in :func:`read_csv` and :func:`read_table`, in a future version the argument will be removed (:issue:`43396`)
Copy link
Contributor

@asishm asishm Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be prefix (and not header)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx

- Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`44398`)
- Deprecated passing non boolean argument to sort in :func:`concat` (:issue:`41518`)
- Deprecated passing ``skipna=None`` for :meth:`DataFrame.mad` and :meth:`Series.mad`, pass ``skipna=True`` instead (:issue:`44580`)
Expand Down
3 changes: 3 additions & 0 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ class _DeprecationConfig(NamedTuple):
"squeeze": _DeprecationConfig(
None, 'Append .squeeze("columns") to the call to squeeze.'
),
"prefix": _DeprecationConfig(
None, "Use a list comprehension on the column names in the future."
),
}


Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/io/parser/common/test_common_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,8 @@ def test_names_and_prefix_not_None_raises(all_parsers, func):
parser = all_parsers
msg = "Specified named and prefix; you can only specify one."
with pytest.raises(ValueError, match=msg):
getattr(parser, func)(f, names=["a", "b"], prefix="x")
with tm.assert_produces_warning(FutureWarning):
getattr(parser, func)(f, names=["a", "b"], prefix="x")


@pytest.mark.parametrize("func", ["read_csv", "read_table"])
Expand All @@ -833,7 +834,10 @@ def test_names_and_prefix_explicit_None(all_parsers, names, prefix, func):
f = StringIO("a,b\n1,2")
expected = DataFrame({"x0": ["a", "1"], "x1": ["b", "2"]})
parser = all_parsers
result = getattr(parser, func)(f, names=names, sep=",", prefix=prefix, header=None)
with tm.assert_produces_warning(FutureWarning):
result = getattr(parser, func)(
f, names=names, sep=",", prefix=prefix, header=None
)
tm.assert_frame_equal(result, expected)


Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/parser/common/test_read_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def test_read_csv_raises_on_header_prefix(all_parsers):
s = StringIO("0,1\n2,3")

with pytest.raises(ValueError, match=msg):
parser.read_csv(s, header=0, prefix="_X")
with tm.assert_produces_warning(FutureWarning):
parser.read_csv(s, header=0, prefix="_X")


def test_unexpected_keyword_parameter_exception(all_parsers):
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/io/parser/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def test_no_header_prefix(all_parsers):
6,7,8,9,10
11,12,13,14,15
"""
result = parser.read_csv(StringIO(data), prefix="Field", header=None)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = parser.read_csv(StringIO(data), prefix="Field", header=None)
expected = DataFrame(
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]],
columns=["Field0", "Field1", "Field2", "Field3", "Field4"],
Expand Down Expand Up @@ -457,7 +458,11 @@ def test_no_header(all_parsers, kwargs, names):
expected = DataFrame(
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]], columns=names
)
result = parser.read_csv(StringIO(data), header=None, **kwargs)
if "prefix" in kwargs.keys():
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = parser.read_csv(StringIO(data), header=None, **kwargs)
else:
result = parser.read_csv(StringIO(data), header=None, **kwargs)
tm.assert_frame_equal(result, expected)


Expand Down
56 changes: 30 additions & 26 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ def date_parser(*date_cols):
"""
return parsing.try_parse_dates(parsing.concat_date_cols(date_cols))

result = parser.read_csv(
StringIO(data),
header=None,
date_parser=date_parser,
prefix="X",
parse_dates={"actual": [1, 2], "nominal": [1, 3]},
keep_date_col=keep_date_col,
)
with tm.assert_produces_warning(FutureWarning):
result = parser.read_csv(
StringIO(data),
header=None,
date_parser=date_parser,
prefix="X",
parse_dates={"actual": [1, 2], "nominal": [1, 3]},
keep_date_col=keep_date_col,
)
expected = DataFrame(
[
[
Expand Down Expand Up @@ -318,13 +319,14 @@ def test_multiple_date_col(all_parsers, keep_date_col):
KORD,19990127, 23:00:00, 22:56:00, -0.5900, 1.7100, 4.6000, 0.0000, 280.0000
"""
parser = all_parsers
result = parser.read_csv(
StringIO(data),
header=None,
prefix="X",
parse_dates=[[1, 2], [1, 3]],
keep_date_col=keep_date_col,
)
with tm.assert_produces_warning(FutureWarning):
result = parser.read_csv(
StringIO(data),
header=None,
prefix="X",
parse_dates=[[1, 2], [1, 3]],
keep_date_col=keep_date_col,
)
expected = DataFrame(
[
[
Expand Down Expand Up @@ -436,9 +438,10 @@ def test_date_col_as_index_col(all_parsers):
KORD,19990127 22:00:00, 21:56:00, -0.5900, 1.7100, 5.1000, 0.0000, 290.0000
"""
parser = all_parsers
result = parser.read_csv(
StringIO(data), header=None, prefix="X", parse_dates=[1], index_col=1
)
with tm.assert_produces_warning(FutureWarning):
result = parser.read_csv(
StringIO(data), header=None, prefix="X", parse_dates=[1], index_col=1
)

index = Index(
[
Expand Down Expand Up @@ -486,14 +489,15 @@ def test_multiple_date_cols_int_cast(all_parsers, date_parser, warning):
parse_dates = {"actual": [1, 2], "nominal": [1, 3]}
parser = all_parsers

with tm.assert_produces_warning(warning, check_stacklevel=False):
result = parser.read_csv(
StringIO(data),
header=None,
date_parser=date_parser,
parse_dates=parse_dates,
prefix="X",
)
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(warning, check_stacklevel=False):
result = parser.read_csv(
StringIO(data),
header=None,
date_parser=date_parser,
parse_dates=parse_dates,
prefix="X",
)

expected = DataFrame(
[
Expand Down