Skip to content

Commit 734e095

Browse files
committed
fix #549 : added argument 'value_name' to to_csv/excel to set the name of the last column (i.e. the one containing the values) when exporting to csv/excel with wide=False
1 parent 61e2bb9 commit 734e095

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

doc/source/changes/version_0_28.rst.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,20 @@ Miscellaneous improvements
269269

270270
* added argument `name` to `to_series` method allowing to set a name to the Pandas Series returned by the method.
271271

272+
* added argument `value_name` to `to_csv` and `to_excel` allowing to change the default name ('value') to
273+
the column containg the values when the argument `wide` is set to False:
274+
275+
>>> arr.to_csv('my_file.csv', wide=False, value_name='data')
276+
a,b,data
277+
a0,b0,0
278+
a0,b1,1
279+
a0,b2,2
280+
a1,b0,3
281+
a1,b1,4
282+
a1,b2,5
283+
284+
Closes :issue:`549`.
285+
272286

273287
Fixes
274288
-----

larray/core/array.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5850,7 +5850,7 @@ def clip(self, a_min, a_max, out=None):
58505850
return clip(self, a_min, a_max, out)
58515851

58525852
@deprecate_kwarg('transpose', 'wide')
5853-
def to_csv(self, filepath, sep=',', na_rep='', wide=True, dropna=None, dialect='default', **kwargs):
5853+
def to_csv(self, filepath, sep=',', na_rep='', wide=True, value_name='value', dropna=None, dialect='default', **kwargs):
58545854
"""
58555855
Writes array to a csv file.
58565856
@@ -5866,6 +5866,9 @@ def to_csv(self, filepath, sep=',', na_rep='', wide=True, dropna=None, dialect='
58665866
Whether or not writing arrays in "wide" format. If True, arrays are exported with the last axis
58675867
represented horizontally. If False, arrays are exported in "narrow" format: one column per axis plus one
58685868
value column. Defaults to True.
5869+
value_name : str, optional
5870+
Name of the column containing the values (last column) in the csv file when `wide=False` (see above).
5871+
Defaults to 'value'.
58695872
dialect : 'default' | 'classic', optional
58705873
Whether or not to write the last axis name (using '\' ). Defaults to 'default'.
58715874
dropna : None, 'all', 'any' or True, optional
@@ -5890,7 +5893,15 @@ def to_csv(self, filepath, sep=',', na_rep='', wide=True, dropna=None, dialect='
58905893
>>> a.to_csv(fname, sep=';', wide=False)
58915894
>>> with open(fname) as f:
58925895
... print(f.read().strip())
5893-
nat;sex;0
5896+
nat;sex;value
5897+
BE;M;0
5898+
BE;F;1
5899+
FO;M;2
5900+
FO;F;3
5901+
>>> a.to_csv(fname, sep=';', wide=False, value_name='population')
5902+
>>> with open(fname) as f:
5903+
... print(f.read().strip())
5904+
nat;sex;population
58945905
BE;M;0
58955906
BE;F;1
58965907
FO;M;2
@@ -5907,7 +5918,7 @@ def to_csv(self, filepath, sep=',', na_rep='', wide=True, dropna=None, dialect='
59075918
frame = self.to_frame(fold, dropna)
59085919
frame.to_csv(filepath, sep=sep, na_rep=na_rep, **kwargs)
59095920
else:
5910-
series = self.to_series(dropna=dropna is not None)
5921+
series = self.to_series(value_name, dropna is not None)
59115922
series.to_csv(filepath, sep=sep, na_rep=na_rep, header=True, **kwargs)
59125923

59135924
def to_hdf(self, filepath, key, *args, **kwargs):
@@ -5935,7 +5946,7 @@ def to_hdf(self, filepath, key, *args, **kwargs):
59355946
self.to_frame().to_hdf(filepath, key, *args, **kwargs)
59365947

59375948
def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file=False, clear_sheet=False,
5938-
header=True, transpose=False, wide=True, engine=None, *args, **kwargs):
5949+
header=True, transpose=False, wide=True, value_name='value', engine=None, *args, **kwargs):
59395950
"""
59405951
Writes array in the specified sheet of specified excel workbook.
59415952
@@ -5964,6 +5975,9 @@ def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file
59645975
Whether or not writing arrays in "wide" format. If True, arrays are exported with the last axis
59655976
represented horizontally. If False, arrays are exported in "narrow" format: one column per axis plus one
59665977
value column. Defaults to True.
5978+
value_name : str, optional
5979+
Name of the column containing the values (last column) in the Excel sheet when `wide=False` (see above).
5980+
Defaults to 'value'.
59675981
engine : 'xlwings' | 'openpyxl' | 'xlsxwriter' | 'xlwt' | None, optional
59685982
Engine to use to make the output. If None (default), it will use 'xlwings' by default if the module is
59695983
installed and relies on Pandas default writer otherwise.
@@ -5985,7 +5999,7 @@ def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file
59855999
if wide:
59866000
pd_obj = self.to_frame(fold_last_axis_name=True)
59876001
else:
5988-
pd_obj = self.to_series()
6002+
pd_obj = self.to_series(value_name)
59896003

59906004
if engine is None:
59916005
engine = 'xlwings' if xw is not None else None
@@ -6021,8 +6035,8 @@ def to_excel(self, filepath=None, sheet_name=None, position='A1', overwrite_file
60216035

60226036
options = dict(header=header, index=header, transpose=transpose)
60236037
sheet[position].options(**options).value = pd_obj
6024-
# TODO: implement transpose via/in dump
6025-
# sheet[position] = self.dump(header=header, transpose=transpose)
6038+
# TODO: implement wide via/in dump
6039+
# sheet[position] = self.dump(header=header, wide=wide)
60266040
if close:
60276041
wb.save()
60286042
wb.close()

larray/tests/test_array.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,6 @@ def test_from_frame(self):
31713171
assert la.axes.names == ['age', 'sex', 'time']
31723172
assert_array_equal(la[0, 'F', :], [3722, 3395, 3347])
31733173

3174-
31753174
def test_to_csv(self):
31763175
la = read_csv(inputpath('test5d.csv'))
31773176
self.assertEqual(la.ndim, 5)
@@ -3189,7 +3188,7 @@ def test_to_csv(self):
31893188

31903189
# stacked data (one column containing all the values and another column listing the context of the value)
31913190
la.to_csv(self.tmp_path('out.csv'), wide=False)
3192-
result = ['arr,age,sex,nat,time,0\n',
3191+
result = ['arr,age,sex,nat,time,value\n',
31933192
'1,0,F,1,2007,3722\n',
31943193
'1,0,F,1,2010,3395\n']
31953194
with open(self.tmp_path('out.csv')) as f:
@@ -3222,7 +3221,7 @@ def test_to_excel_xlsxwriter(self):
32223221
# stacked data (one column containing all the values and another column listing the context of the value)
32233222
a1.to_excel(fpath, wide=False, engine='xlsxwriter')
32243223
res = read_excel(fpath, engine='xlrd')
3225-
stacked_a1 = a1.reshape([a1.a, Axis([0])])
3224+
stacked_a1 = a1.reshape([a1.a, Axis(['value'])])
32263225
assert_array_equal(res, stacked_a1)
32273226

32283227
# 2D
@@ -3282,7 +3281,7 @@ def test_to_excel_xlsxwriter(self):
32823281
# stacked data (one column containing all the values and another column listing the context of the value)
32833282
a1.to_excel(fpath, wide=False, engine='xlsxwriter')
32843283
res = read_excel(fpath, engine='xlrd')
3285-
stacked_a1 = a1.reshape([a1.a, Axis([0])])
3284+
stacked_a1 = a1.reshape([a1.a, Axis(['value'])])
32863285
assert_array_equal(res, stacked_a1)
32873286

32883287
# 2D

0 commit comments

Comments
 (0)