Skip to content

Commit fa8a35c

Browse files
committed
Fix pandas-dev#6645. Raises warning on use of cols and update unit tests
1 parent 80cef54 commit fa8a35c

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

doc/source/release.rst

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ API Changes
134134
``FutureWarning`` is raised to alert that the old ``rows`` and ``cols`` arguments
135135
will not be supported in a future release (:issue:`5505`)
136136

137+
- The :meth:`DataFrame.to_csv` and :meth:`DataFrame.to_excel` functions
138+
now takes argument ``columns`` instead of ``cols``. A
139+
``FutureWarning`` is raised to alert that the old ``cols`` arguments
140+
will not be supported in a future release (:issue:`6645`)
141+
137142
- Allow specification of a more complex groupby, via ``pd.Grouper`` (:issue:`3794`)
138143

139144
- A tuple passed to ``DataFame.sort_index`` will be interpreted as the levels of

doc/source/v0.14.0.txt

+5
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ These are out-of-bounds selections
178178
``FutureWarning`` is raised to alert that the old ``rows`` and ``cols`` arguments
179179
will not be supported in a future release (:issue:`5505`)
180180

181+
- The :meth:`DataFrame.to_csv` and :meth:`DataFrame.to_excel` functions
182+
now takes argument ``columns`` instead of ``cols``. A
183+
``FutureWarning`` is raised to alert that the old ``cols`` arguments
184+
will not be supported in a future release (:issue:`6645`)
185+
181186
- Following keywords are now acceptable for :meth:`DataFrame.plot(kind='bar')` and :meth:`DataFrame.plot(kind='barh')`.
182187
- `width`: Specify the bar width. In previous versions, static value 0.5 was passed to matplotlib and it cannot be overwritten.
183188
- `position`: Specify relative alignments for bar plot layout. From 0 (left/bottom-end) to 1(right/top-end). Default is 0.5 (center). (:issue:`6604`)

pandas/core/frame.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ def to_panel(self):
10681068
to_wide = deprecate('to_wide', to_panel)
10691069

10701070
def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
1071-
cols=None, header=True, index=True, index_label=None,
1071+
columns=None, header=True, index=True, index_label=None,
10721072
mode='w', nanRep=None, encoding=None, quoting=None,
10731073
quotechar='"', line_terminator='\n', chunksize=None,
10741074
tupleize_cols=False, date_format=None, doublequote=True,
@@ -1086,7 +1086,7 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
10861086
Missing data representation
10871087
float_format : string, default None
10881088
Format string for floating point numbers
1089-
cols : sequence, optional
1089+
columns : sequence, optional
10901090
Columns to write
10911091
header : boolean or list of string, default True
10921092
Write out column names. If a list of string is given it is assumed
@@ -1124,17 +1124,29 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
11241124
or new (expanded format) if False)
11251125
date_format : string, default None
11261126
Format string for datetime objects
1127+
cols : kwarg only alias of columns [deprecated]
11271128
"""
11281129
if nanRep is not None: # pragma: no cover
11291130
warnings.warn("nanRep is deprecated, use na_rep",
11301131
FutureWarning)
11311132
na_rep = nanRep
11321133

1134+
# Parse old-style keyword argument
1135+
cols = kwds.pop('cols', None)
1136+
if cols is not None:
1137+
warnings.warn("cols is deprecated, use columns", FutureWarning)
1138+
if columns is None:
1139+
columns = cols
1140+
else:
1141+
msg = "Can only specify either 'columns' or 'cols'"
1142+
raise TypeError(msg)
1143+
1144+
11331145
formatter = fmt.CSVFormatter(self, path_or_buf,
11341146
line_terminator=line_terminator,
11351147
sep=sep, encoding=encoding,
11361148
quoting=quoting, na_rep=na_rep,
1137-
float_format=float_format, cols=cols,
1149+
float_format=float_format, cols=columns,
11381150
header=header, index=index,
11391151
index_label=index_label, mode=mode,
11401152
chunksize=chunksize, quotechar=quotechar,
@@ -1149,9 +1161,9 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
11491161
return formatter.path_or_buf.getvalue()
11501162

11511163
def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
1152-
float_format=None, cols=None, header=True, index=True,
1164+
float_format=None, columns=None, header=True, index=True,
11531165
index_label=None, startrow=0, startcol=0, engine=None,
1154-
merge_cells=True, encoding=None):
1166+
merge_cells=True, encoding=None, **kwds):
11551167
"""
11561168
Write DataFrame to a excel sheet
11571169
@@ -1189,6 +1201,7 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
11891201
encoding: string, default None
11901202
encoding of the resulting excel file. Only necessary for xlwt,
11911203
other writers support unicode natively.
1204+
cols : kwarg only alias of columns [deprecated]
11921205
11931206
Notes
11941207
-----
@@ -1202,6 +1215,17 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
12021215
>>> writer.save()
12031216
"""
12041217
from pandas.io.excel import ExcelWriter
1218+
1219+
# Parse old-style keyword argument
1220+
cols = kwds.pop('cols', None)
1221+
if cols is not None:
1222+
warnings.warn("cols is deprecated, use columns", FutureWarning)
1223+
if columns is None:
1224+
columns = cols
1225+
else:
1226+
msg = "Can only specify either 'columns' or 'cols'"
1227+
raise TypeError(msg)
1228+
12051229
need_save = False
12061230
if encoding == None:
12071231
encoding = 'ascii'
@@ -1212,7 +1236,7 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
12121236

12131237
formatter = fmt.ExcelFormatter(self,
12141238
na_rep=na_rep,
1215-
cols=cols,
1239+
cols=columns,
12161240
header=header,
12171241
float_format=float_format,
12181242
index=index,

pandas/io/tests/test_excel.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ def test_excel_sheet_by_name_raise(self):
396396

397397
self.assertRaises(xlrd.XLRDError, xl.parse, '0')
398398

399+
def test_excel_deprecated_options(self):
400+
with ensure_clean(self.ext) as path:
401+
with tm.assert_produces_warning(FutureWarning):
402+
self.frame.to_excel(path, 'test1', cols=['A', 'B'])
403+
404+
with tm.assert_produces_warning(False):
405+
self.frame.to_excel(path, 'test1', columns=['A', 'B'])
406+
399407
def test_excelwriter_contextmanager(self):
400408
_skip_if_no_xlrd()
401409

@@ -417,7 +425,7 @@ def test_roundtrip(self):
417425
self.frame['A'][:5] = nan
418426

419427
self.frame.to_excel(path, 'test1')
420-
self.frame.to_excel(path, 'test1', cols=['A', 'B'])
428+
self.frame.to_excel(path, 'test1', columns=['A', 'B'])
421429
self.frame.to_excel(path, 'test1', header=False)
422430
self.frame.to_excel(path, 'test1', index=False)
423431

@@ -479,7 +487,7 @@ def test_basics_with_nan(self):
479487
with ensure_clean(self.ext) as path:
480488
self.frame['A'][:5] = nan
481489
self.frame.to_excel(path, 'test1')
482-
self.frame.to_excel(path, 'test1', cols=['A', 'B'])
490+
self.frame.to_excel(path, 'test1', columns=['A', 'B'])
483491
self.frame.to_excel(path, 'test1', header=False)
484492
self.frame.to_excel(path, 'test1', index=False)
485493

@@ -537,7 +545,7 @@ def test_sheets(self):
537545
self.frame['A'][:5] = nan
538546

539547
self.frame.to_excel(path, 'test1')
540-
self.frame.to_excel(path, 'test1', cols=['A', 'B'])
548+
self.frame.to_excel(path, 'test1', columns=['A', 'B'])
541549
self.frame.to_excel(path, 'test1', header=False)
542550
self.frame.to_excel(path, 'test1', index=False)
543551

@@ -562,7 +570,7 @@ def test_colaliases(self):
562570
self.frame['A'][:5] = nan
563571

564572
self.frame.to_excel(path, 'test1')
565-
self.frame.to_excel(path, 'test1', cols=['A', 'B'])
573+
self.frame.to_excel(path, 'test1', columns=['A', 'B'])
566574
self.frame.to_excel(path, 'test1', header=False)
567575
self.frame.to_excel(path, 'test1', index=False)
568576

@@ -583,7 +591,7 @@ def test_roundtrip_indexlabels(self):
583591
self.frame['A'][:5] = nan
584592

585593
self.frame.to_excel(path, 'test1')
586-
self.frame.to_excel(path, 'test1', cols=['A', 'B'])
594+
self.frame.to_excel(path, 'test1', columns=['A', 'B'])
587595
self.frame.to_excel(path, 'test1', header=False)
588596
self.frame.to_excel(path, 'test1', index=False)
589597

@@ -630,7 +638,7 @@ def test_roundtrip_indexlabels(self):
630638

631639
self.frame.to_excel(path,
632640
'test1',
633-
cols=['A', 'B', 'C', 'D'],
641+
columns=['A', 'B', 'C', 'D'],
634642
index=False, merge_cells=self.merge_cells)
635643
# take 'A' and 'B' as indexes (same row as cols 'C', 'D')
636644
df = self.frame.copy()
@@ -733,7 +741,7 @@ def test_to_excel_multiindex(self):
733741

734742
with ensure_clean(self.ext) as path:
735743
frame.to_excel(path, 'test1', header=False)
736-
frame.to_excel(path, 'test1', cols=['A', 'B'])
744+
frame.to_excel(path, 'test1', columns=['A', 'B'])
737745

738746
# round trip
739747
frame.to_excel(path, 'test1', merge_cells=self.merge_cells)
@@ -1020,7 +1028,7 @@ def test_swapped_columns(self):
10201028
with ensure_clean(self.ext) as path:
10211029
write_frame = DataFrame({'A': [1, 1, 1],
10221030
'B': [2, 2, 2]})
1023-
write_frame.to_excel(path, 'test1', cols=['B', 'A'])
1031+
write_frame.to_excel(path, 'test1', columns=['B', 'A'])
10241032

10251033
read_frame = read_excel(path, 'test1', header=0)
10261034

pandas/tests/test_frame.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -5407,6 +5407,13 @@ def test_to_csv_deprecated_options(self):
54075407
self.tsframe.to_csv(path, nanRep='foo')
54085408
recons = read_csv(path,index_col=0,parse_dates=[0],na_values=['foo'])
54095409
assert_frame_equal(self.tsframe, recons)
5410+
5411+
with tm.assert_produces_warning(FutureWarning):
5412+
self.frame.to_csv(path, cols=['A', 'B'])
5413+
5414+
with tm.assert_produces_warning(False):
5415+
self.frame.to_csv(path, columns=['A', 'B'])
5416+
54105417

54115418
def test_to_csv_from_csv(self):
54125419

@@ -5416,7 +5423,7 @@ def test_to_csv_from_csv(self):
54165423
self.frame['A'][:5] = nan
54175424

54185425
self.frame.to_csv(path)
5419-
self.frame.to_csv(path, cols=['A', 'B'])
5426+
self.frame.to_csv(path, columns=['A', 'B'])
54205427
self.frame.to_csv(path, header=False)
54215428
self.frame.to_csv(path, index=False)
54225429

@@ -5490,9 +5497,9 @@ def test_to_csv_cols_reordering(self):
54905497

54915498
def _check_df(df,cols=None):
54925499
with ensure_clean() as path:
5493-
df.to_csv(path,cols = cols,engine='python')
5500+
df.to_csv(path,columns = cols,engine='python')
54945501
rs_p = pd.read_csv(path,index_col=0)
5495-
df.to_csv(path,cols = cols,chunksize=chunksize)
5502+
df.to_csv(path,columns = cols,chunksize=chunksize)
54965503
rs_c = pd.read_csv(path,index_col=0)
54975504

54985505
if cols:
@@ -5518,7 +5525,7 @@ def test_to_csv_new_dupe_cols(self):
55185525
import pandas as pd
55195526
def _check_df(df,cols=None):
55205527
with ensure_clean() as path:
5521-
df.to_csv(path,cols = cols,chunksize=chunksize)
5528+
df.to_csv(path,columns = cols,chunksize=chunksize)
55225529
rs_c = pd.read_csv(path,index_col=0)
55235530

55245531
# we wrote them in a different order
@@ -5775,7 +5782,7 @@ def test_to_csv_multiindex(self):
57755782
with ensure_clean(pname) as path:
57765783

57775784
frame.to_csv(path, header=False)
5778-
frame.to_csv(path, cols=['A', 'B'])
5785+
frame.to_csv(path, columns=['A', 'B'])
57795786

57805787
# round trip
57815788
frame.to_csv(path)
@@ -5893,7 +5900,7 @@ def _make_frame(names=None):
58935900

58945901
# write with cols
58955902
with assertRaisesRegexp(TypeError, 'cannot specify cols with a MultiIndex'):
5896-
df.to_csv(path, tupleize_cols=False, cols=['foo', 'bar'])
5903+
df.to_csv(path, tupleize_cols=False, columns=['foo', 'bar'])
58975904

58985905
with ensure_clean(pname) as path:
58995906
# empty

0 commit comments

Comments
 (0)