Skip to content

Commit f6504dd

Browse files
committed
fix #575 and fix #371 : renamed argument 'transpose' of to_csv as 'wide' + added argument 'wide' to to_excel
1 parent 4ea5f3b commit f6504dd

File tree

3 files changed

+84
-20
lines changed

3 files changed

+84
-20
lines changed

doc/source/changes/version_0_28.rst.inc

+34-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ Miscellaneous improvements
211211
a0 0 0
212212
a1 1 1
213213

214-
215214
* renamed argument `nb_index` of `read_csv`, `read_excel`, `read_sas`, `from_lists` and `from_string` functions
216215
as `nb_axes`. The relation between `nb_index` and `nb_axes` is given by `nb_axes = nb_index + 1`:
217216

@@ -234,6 +233,40 @@ Miscellaneous improvements
234233

235234
Closes :issue:`548`:
236235

236+
* renamed argument `transpose` by `wide` in `to_csv` method.
237+
238+
* added argument `wide` in `to_excel` method. When argument `wide` is set to False, the array is exported
239+
in "narrow" format, i.e. one column per axis plus one value column:
240+
241+
>>> arr = ndtest((2, 3))
242+
>>> arr
243+
a\b b0 b1 b2
244+
a0 0 1 2
245+
a1 3 4 5
246+
247+
Default behavior (`wide=True`):
248+
249+
>>> arr.to_excel('my_file.xlsx')
250+
251+
a\b b0 b1 b2
252+
a0 0 1 2
253+
a1 3 4 5
254+
255+
With `wide=False`:
256+
257+
>>> arr.to_excel('my_file.xlsx', wide=False)
258+
259+
a b value
260+
a0 b0 0
261+
a0 b1 1
262+
a0 b2 2
263+
a1 b0 3
264+
a1 b1 4
265+
a1 b2 5
266+
267+
Argument `transpose` has a different purpose than `wide` and is mainly useful to allow multiple axes as header
268+
when exporting arrays with more than 2 dimensions. Closes :issue:`575` and :issue:`371`.
269+
237270

238271
Fixes
239272
-----

larray/core/array.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -5816,23 +5816,25 @@ def clip(self, a_min, a_max, out=None):
58165816
from larray.core.ufuncs import clip
58175817
return clip(self, a_min, a_max, out)
58185818

5819-
def to_csv(self, filepath, sep=',', na_rep='', transpose=True, dropna=None, dialect='default', **kwargs):
5819+
@deprecate_kwarg('transpose', 'wide')
5820+
def to_csv(self, filepath, sep=',', na_rep='', wide=True, dropna=None, dialect='default', **kwargs):
58205821
"""
58215822
Writes array to a csv file.
58225823
58235824
Parameters
58245825
----------
58255826
filepath : str
58265827
path where the csv file has to be written.
5827-
sep : str
5828-
seperator for the csv file.
5829-
na_rep : str
5830-
replace NA values with na_rep.
5831-
transpose : boolean
5832-
transpose = True => transpose over last axis.
5833-
transpose = False => no transpose.
5834-
dialect : 'default' | 'classic'
5835-
Whether or not to write the last axis name (using '\' )
5828+
sep : str, optional
5829+
separator for the csv file. Defaults to `,`.
5830+
na_rep : str, optional
5831+
replace NA values with na_rep. Defaults to ''.
5832+
wide : boolean, optional
5833+
Whether or not writing arrays in "wide" format. If True, arrays are exported with the last axis
5834+
represented horizontally. If False, arrays are exported in "narrow" format: one column per axis plus one
5835+
value column. Defaults to True.
5836+
dialect : 'default' | 'classic', optional
5837+
Whether or not to write the last axis name (using '\' ). Defaults to 'default'.
58365838
dropna : None, 'all', 'any' or True, optional
58375839
Drop lines if 'all' its values are NA, if 'any' value is NA or do not drop any line (default).
58385840
True is equivalent to 'all'.
@@ -5852,7 +5854,7 @@ def to_csv(self, filepath, sep=',', na_rep='', transpose=True, dropna=None, dial
58525854
nat\\sex,M,F
58535855
BE,0,1
58545856
FO,2,3
5855-
>>> a.to_csv(fname, sep=';', transpose=False)
5857+
>>> a.to_csv(fname, sep=';', wide=False)
58565858
>>> with open(fname) as f:
58575859
... print(f.read().strip())
58585860
nat;sex;0
@@ -5868,7 +5870,7 @@ def to_csv(self, filepath, sep=',', na_rep='', transpose=True, dropna=None, dial
58685870
FO,2,3
58695871
"""
58705872
fold = dialect == 'default'
5871-
if transpose:
5873+
if wide:
58725874
frame = self.to_frame(fold, dropna)
58735875
frame.to_csv(filepath, sep=sep, na_rep=na_rep, **kwargs)
58745876
else:
@@ -5900,7 +5902,7 @@ def to_hdf(self, filepath, key, *args, **kwargs):
59005902
self.to_frame().to_hdf(filepath, key, *args, **kwargs)
59015903

59025904
def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file=False, clear_sheet=False,
5903-
header=True, transpose=False, engine=None, *args, **kwargs):
5905+
header=True, transpose=False, wide=True, engine=None, *args, **kwargs):
59045906
"""
59055907
Writes array in the specified sheet of specified excel workbook.
59065908
@@ -5923,8 +5925,12 @@ def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file
59235925
header : bool, optional
59245926
Whether or not to write a header (axes names and labels). Defaults to True.
59255927
transpose : bool, optional
5926-
Whether or not to transpose the resulting array. This can be used, for example, for writing one dimensional
5927-
arrays vertically. Defaults to False.
5928+
Whether or not to transpose the array transpose over last axis.
5929+
This is equivalent to paste with option transpose in Excel. Defaults to False.
5930+
wide : boolean, optional
5931+
Whether or not writing arrays in "wide" format. If True, arrays are exported with the last axis
5932+
represented horizontally. If False, arrays are exported in "narrow" format: one column per axis plus one
5933+
value column. Defaults to True.
59285934
engine : 'xlwings' | 'openpyxl' | 'xlsxwriter' | 'xlwt' | None, optional
59295935
Engine to use to make the output. If None (default), it will use 'xlwings' by default if the module is
59305936
installed and relies on Pandas default writer otherwise.
@@ -5943,7 +5949,11 @@ def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file
59435949
"""
59445950
sheet_name = _translate_sheet_name(sheet_name)
59455951

5946-
df = self.to_frame(fold_last_axis_name=True)
5952+
if wide:
5953+
pd_obj = self.to_frame(fold_last_axis_name=True)
5954+
else:
5955+
pd_obj = self.to_series()
5956+
59475957
if engine is None:
59485958
engine = 'xlwings' if xw is not None else None
59495959

@@ -5977,7 +5987,7 @@ def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file
59775987
sheet = wb.sheets.add(sheet_name, after=wb.sheets[-1])
59785988

59795989
options = dict(header=header, index=header, transpose=transpose)
5980-
sheet[position].options(**options).value = df
5990+
sheet[position].options(**options).value = pd_obj
59815991
# TODO: implement transpose via/in dump
59825992
# sheet[position] = self.dump(header=header, transpose=transpose)
59835993
if close:
@@ -5988,7 +5998,7 @@ def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file
59885998
sheet_name = 'Sheet1'
59895999
# TODO: implement position in this case
59906000
# startrow, startcol
5991-
df.to_excel(filepath, sheet_name, *args, engine=engine, **kwargs)
6001+
pd_obj.to_excel(filepath, sheet_name, *args, engine=engine, **kwargs)
59926002

59936003
def to_clipboard(self, *args, **kwargs):
59946004
"""Sends the content of the array to clipboard.

larray/tests/test_array.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -3187,7 +3187,8 @@ def test_to_csv(self):
31873187
with open(self.tmp_path('out.csv')) as f:
31883188
self.assertEqual(f.readlines()[:3], result)
31893189

3190-
la.to_csv(self.tmp_path('out.csv'), transpose=False)
3190+
# stacked data (one column containing all the values and another column listing the context of the value)
3191+
la.to_csv(self.tmp_path('out.csv'), wide=False)
31913192
result = ['arr,age,sex,nat,time,0\n',
31923193
'1,0,F,1,2007,3722\n',
31933194
'1,0,F,1,2010,3395\n']
@@ -3217,6 +3218,13 @@ def test_to_excel_xlsxwriter(self):
32173218
res = read_excel(fpath, engine='xlrd')
32183219
assert_array_equal(res, a1)
32193220

3221+
# fpath/Sheet1/A1
3222+
# stacked data (one column containing all the values and another column listing the context of the value)
3223+
a1.to_excel(fpath, wide=False, engine='xlsxwriter')
3224+
res = read_excel(fpath, engine='xlrd')
3225+
stacked_a1 = a1.reshape([a1.a, Axis([0])])
3226+
assert_array_equal(res, stacked_a1)
3227+
32203228
# 2D
32213229
a2 = ndtest((2, 3))
32223230

@@ -3270,6 +3278,13 @@ def test_to_excel_xlsxwriter(self):
32703278
res = read_excel(fpath, engine='xlrd')
32713279
assert_array_equal(res, a1)
32723280

3281+
# fpath/Sheet1/A1
3282+
# stacked data (one column containing all the values and another column listing the context of the value)
3283+
a1.to_excel(fpath, wide=False, engine='xlsxwriter')
3284+
res = read_excel(fpath, engine='xlrd')
3285+
stacked_a1 = a1.reshape([a1.a, Axis([0])])
3286+
assert_array_equal(res, stacked_a1)
3287+
32733288
# 2D
32743289
a2 = ndtest((2, 3))
32753290

@@ -3352,6 +3367,12 @@ def test_to_excel_xlwings(self):
33523367
res = read_excel(fpath, engine='xlrd')
33533368
assert_array_equal(res, a1)
33543369

3370+
# fpath/Sheet1/A1
3371+
# stacked data (one column containing all the values and another column listing the context of the value)
3372+
a1.to_excel(fpath, wide=False, engine='xlwings')
3373+
res = read_excel(fpath, engine='xlrd')
3374+
assert_array_equal(res, a1)
3375+
33553376
# 2D
33563377
a2 = ndtest((2, 3))
33573378

0 commit comments

Comments
 (0)