Skip to content

Commit 7c8bf8d

Browse files
gfyoungPingviinituutti
authored andcommitted
DEPR: Deprecate usecols as int in read_excel (pandas-dev#23635)
Follow-up to pandas-devgh-23544.
1 parent 5369575 commit 7c8bf8d

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

doc/source/io.rst

+5
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,11 @@ It is often the case that users will insert columns to do temporary computations
28542854
in Excel and you may not want to read in those columns. ``read_excel`` takes
28552855
a ``usecols`` keyword to allow you to specify a subset of columns to parse.
28562856

2857+
.. deprecated:: 0.24.0
2858+
2859+
Passing in an integer for ``usecols`` has been deprecated. Please pass in a list
2860+
of ints from 0 to ``usecols`` inclusive instead.
2861+
28572862
If ``usecols`` is an integer, then it is assumed to indicate the last column
28582863
to be parsed.
28592864

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ Deprecations
970970
- The class ``FrozenNDArray`` has been deprecated. When unpickling, ``FrozenNDArray`` will be unpickled to ``np.ndarray`` once this class is removed (:issue:`9031`)
971971
- Deprecated the `nthreads` keyword of :func:`pandas.read_feather` in favor of
972972
`use_threads` to reflect the changes in pyarrow 0.11.0. (:issue:`23053`)
973+
- :func:`pandas.read_excel` has deprecated accepting ``usecols`` as an integer. Please pass in a list of ints from 0 to ``usecols`` inclusive instead (:issue:`23527`)
973974
- Constructing a :class:`TimedeltaIndex` from data with ``datetime64``-dtyped data is deprecated, will raise ``TypeError`` in a future version (:issue:`23539`)
974975

975976
.. _whatsnew_0240.deprecations.datetimelike_int_ops:

pandas/io/excel.py

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
usecols : int, str, list-like, or callable default None
9696
* If None, then parse all columns,
9797
* If int, then indicates last column to be parsed
98+
99+
.. deprecated:: 0.24.0
100+
Pass in a list of ints instead from 0 to `usecols` inclusive.
101+
98102
* If string, then indicates comma separated list of Excel column letters
99103
and column ranges (e.g. "A:E" or "A,C,E:F"). Ranges are inclusive of
100104
both sides.
@@ -778,6 +782,10 @@ def _maybe_convert_usecols(usecols):
778782
return usecols
779783

780784
if is_integer(usecols):
785+
warnings.warn(("Passing in an integer for `usecols` has been "
786+
"deprecated. Please pass in a list of ints from "
787+
"0 to `usecols` inclusive instead."),
788+
FutureWarning, stacklevel=2)
781789
return lrange(usecols + 1)
782790

783791
if isinstance(usecols, compat.string_types):

pandas/tests/io/test_excel.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,34 @@ def get_exceldf(self, basename, ext, *args, **kwds):
105105
class ReadingTestsBase(SharedItems):
106106
# This is based on ExcelWriterBase
107107

108-
@td.skip_if_no('xlrd', '1.0.1') # GH-22682
108+
@td.skip_if_no("xlrd", "1.0.1") # see gh-22682
109109
def test_usecols_int(self, ext):
110110

111-
dfref = self.get_csv_refdf('test1')
112-
dfref = dfref.reindex(columns=['A', 'B', 'C'])
113-
df1 = self.get_exceldf('test1', ext, 'Sheet1', index_col=0, usecols=3)
114-
df2 = self.get_exceldf('test1', ext, 'Sheet2', skiprows=[1],
115-
index_col=0, usecols=3)
111+
df_ref = self.get_csv_refdf("test1")
112+
df_ref = df_ref.reindex(columns=["A", "B", "C"])
116113

117-
with tm.assert_produces_warning(FutureWarning):
118-
df3 = self.get_exceldf('test1', ext, 'Sheet2', skiprows=[1],
114+
# usecols as int
115+
with tm.assert_produces_warning(FutureWarning,
116+
check_stacklevel=False):
117+
df1 = self.get_exceldf("test1", ext, "Sheet1",
118+
index_col=0, usecols=3)
119+
120+
# usecols as int
121+
with tm.assert_produces_warning(FutureWarning,
122+
check_stacklevel=False):
123+
df2 = self.get_exceldf("test1", ext, "Sheet2", skiprows=[1],
124+
index_col=0, usecols=3)
125+
126+
# parse_cols instead of usecols, usecols as int
127+
with tm.assert_produces_warning(FutureWarning,
128+
check_stacklevel=False):
129+
df3 = self.get_exceldf("test1", ext, "Sheet2", skiprows=[1],
119130
index_col=0, parse_cols=3)
120131

121132
# TODO add index to xls file)
122-
tm.assert_frame_equal(df1, dfref, check_names=False)
123-
tm.assert_frame_equal(df2, dfref, check_names=False)
124-
tm.assert_frame_equal(df3, dfref, check_names=False)
133+
tm.assert_frame_equal(df1, df_ref, check_names=False)
134+
tm.assert_frame_equal(df2, df_ref, check_names=False)
135+
tm.assert_frame_equal(df3, df_ref, check_names=False)
125136

126137
@td.skip_if_no('xlrd', '1.0.1') # GH-22682
127138
def test_usecols_list(self, ext):

0 commit comments

Comments
 (0)