Skip to content

Commit 4106646

Browse files
committed
fix #820 : added argument _axes_display_names to the Array.dump() function to specify if exported axes names are defined by the AxisCollection.names or AxisCollection.display_names property
1 parent 50d82b5 commit 4106646

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

larray/core/array.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,7 +2415,8 @@ def __str__(self):
24152415
elif not len(self):
24162416
return 'Array([])'
24172417
else:
2418-
table = self.dump(maxlines=_OPTIONS[DISPLAY_MAXLINES], edgeitems=_OPTIONS[DISPLAY_EDGEITEMS])
2418+
table = self.dump(maxlines=_OPTIONS[DISPLAY_MAXLINES], edgeitems=_OPTIONS[DISPLAY_EDGEITEMS],
2419+
_axes_display_names=True)
24192420
return table2str(table, 'nan', maxwidth=_OPTIONS[DISPLAY_WIDTH], keepcols=self.ndim - 1,
24202421
precision=_OPTIONS[DISPLAY_PRECISION])
24212422
__repr__ = __str__
@@ -2436,11 +2437,12 @@ def as_table(self, maxlines=-1, edgeitems=5, light=False, wide=True, value_name=
24362437
"""
24372438
warnings.warn("Array.as_table() is deprecated. Please use Array.dump() instead.", FutureWarning,
24382439
stacklevel=2)
2439-
return self.dump(maxlines=maxlines, edgeitems=edgeitems, light=light, wide=wide, value_name=value_name)
2440+
return self.dump(maxlines=maxlines, edgeitems=edgeitems, light=light, wide=wide, value_name=value_name,
2441+
_axes_display_names=True)
24402442

24412443
# XXX: dump as a 2D Array with row & col dims?
24422444
def dump(self, header=True, wide=True, value_name='value', light=False, axes_names=True, na_repr='as_is',
2443-
maxlines=-1, edgeitems=5):
2445+
maxlines=-1, edgeitems=5, _axes_display_names=False):
24442446
r"""
24452447
Dump array as a 2D nested list. This is especially useful when writing to an Excel sheet via open_excel().
24462448
@@ -2462,12 +2464,15 @@ def dump(self, header=True, wide=True, value_name='value', light=False, axes_nam
24622464
Assuming header is True, whether or not to include axes names. If axes_names is 'except_last',
24632465
all axes names will be included except the last. Defaults to True.
24642466
na_repr : any scalar, optional
2465-
Replace missing values (NaN floats) by this value. Default to 'as_is' (do not do any replacement).
2467+
Replace missing values (NaN floats) by this value. Defaults to 'as_is' (do not do any replacement).
24662468
maxlines : int, optional
24672469
Maximum number of lines to show. Defaults to -1 (all lines are shown).
24682470
edgeitems : int, optional
24692471
If number of lines to display is greater than `maxlines`, only the first and last `edgeitems` lines are
24702472
displayed. Only active if `maxlines` is not -1. Defaults to 5.
2473+
_axes_display_names : bool, optional
2474+
Whether or not to get axes names using :py:obj:`~AxisCollection.display_names` instead of
2475+
:py:obj:`~AxisCollection.names`. Defaults to False.
24712476
24722477
Returns
24732478
-------
@@ -2516,7 +2521,7 @@ def dump(self, header=True, wide=True, value_name='value', light=False, axes_nam
25162521
['...', '...', '...', '...'],
25172522
['a1', 'b1', 6, 7]]
25182523
"""
2519-
display_axes_names = axes_names
2524+
dump_axes_names = axes_names
25202525

25212526
if not header:
25222527
# ensure_no_numpy_type is there mostly to avoid problems with xlwings, but I am unsure where that problem
@@ -2540,14 +2545,18 @@ def dump(self, header=True, wide=True, value_name='value', light=False, axes_nam
25402545
data = self.data.reshape(height, width)
25412546

25422547
# get list of names of axes
2543-
axes_names = self.axes.display_names[:]
2548+
if _axes_display_names:
2549+
axes_names = self.axes.display_names[:]
2550+
else:
2551+
axes_names = [axis_name if axis_name is not None else '' for axis_name in self.axes.names]
25442552

25452553
# transforms ['a', 'b', 'c', 'd'] into ['a', 'b', 'c\\d']
25462554
if wide and len(axes_names) > 1:
2547-
if display_axes_names is True:
2548-
axes_names[-2] = '\\'.join(axes_names[-2:])
2555+
if dump_axes_names is True:
2556+
separator = '\\' if axes_names[-2] and axes_names[-1] else ''
2557+
axes_names[-2] = separator.join(axes_names[-2:])
25492558
axes_names.pop()
2550-
elif display_axes_names == 'except_last':
2559+
elif dump_axes_names == 'except_last':
25512560
axes_names = axes_names[:-1]
25522561
else:
25532562
axes_names = [''] * (len(axes_names) - 1)

larray/tests/test_array.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,6 +4134,16 @@ def test_dump():
41344134
['a1', 1],
41354135
['a2', 2]]
41364136

4137+
arr = ndtest((Axis('a0,a1'), Axis(2, 'b')))
4138+
res = arr.dump()
4139+
assert res == [['b', 0, 1],
4140+
['a0', 0, 1],
4141+
['a1', 2, 3]]
4142+
res = arr.dump(_axes_display_names=True)
4143+
assert res == [['{0}\\b*', 0, 1],
4144+
['a0', 0, 1],
4145+
['a1', 2, 3]]
4146+
41374147

41384148
@needs_xlwings
41394149
def test_open_excel(tmpdir):

0 commit comments

Comments
 (0)