Skip to content

Commit 0c11567

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 a321af7 commit 0c11567

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

larray/core/array.py

Lines changed: 22 additions & 10 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,12 +2437,15 @@ 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):
2444-
r"""
2445+
maxlines=-1, edgeitems=5, _axes_display_names=False):
2446+
r"""dump(self, header=True, wide=True, value_name='value', light=False, axes_names=True, na_repr='as_is',
2447+
maxlines=-1, edgeitems=5)
2448+
24452449
Dump array as a 2D nested list. This is especially useful when writing to an Excel sheet via open_excel().
24462450
24472451
Parameters
@@ -2462,7 +2466,7 @@ def dump(self, header=True, wide=True, value_name='value', light=False, axes_nam
24622466
Assuming header is True, whether or not to include axes names. If axes_names is 'except_last',
24632467
all axes names will be included except the last. Defaults to True.
24642468
na_repr : any scalar, optional
2465-
Replace missing values (NaN floats) by this value. Default to 'as_is' (do not do any replacement).
2469+
Replace missing values (NaN floats) by this value. Defaults to 'as_is' (do not do any replacement).
24662470
maxlines : int, optional
24672471
Maximum number of lines to show. Defaults to -1 (all lines are shown).
24682472
edgeitems : int, optional
@@ -2516,7 +2520,11 @@ def dump(self, header=True, wide=True, value_name='value', light=False, axes_nam
25162520
['...', '...', '...', '...'],
25172521
['a1', 'b1', 6, 7]]
25182522
"""
2519-
display_axes_names = axes_names
2523+
# _axes_display_names : bool, optional
2524+
# Whether or not to get axes names using AxisCollection.display_names instead of
2525+
# AxisCollection.names. Defaults to False.
2526+
2527+
dump_axes_names = axes_names
25202528

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

25422550
# get list of names of axes
2543-
axes_names = self.axes.display_names[:]
2551+
if _axes_display_names:
2552+
axes_names = self.axes.display_names[:]
2553+
else:
2554+
axes_names = [axis_name if axis_name is not None else '' for axis_name in self.axes.names]
25442555

25452556
# transforms ['a', 'b', 'c', 'd'] into ['a', 'b', 'c\\d']
25462557
if wide and len(axes_names) > 1:
2547-
if display_axes_names is True:
2548-
axes_names[-2] = '\\'.join(axes_names[-2:])
2558+
if dump_axes_names is True:
2559+
separator = '\\' if axes_names[-1] else ''
2560+
axes_names[-2] = separator.join(axes_names[-2:])
25492561
axes_names.pop()
2550-
elif display_axes_names == 'except_last':
2562+
elif dump_axes_names == 'except_last':
25512563
axes_names = axes_names[:-1]
25522564
else:
25532565
axes_names = [''] * (len(axes_names) - 1)

larray/inout/pandas.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def from_frame(df, sort_rows=False, sort_columns=False, parse_header=False, unfo
226226
if unfold_last_axis_name:
227227
if isinstance(axes_names[-1], basestring) and '\\' in axes_names[-1]:
228228
last_axes = [name.strip() for name in axes_names[-1].split('\\')]
229+
last_axes = [name if name else None for name in last_axes]
229230
axes_names = axes_names[:-1] + last_axes
230231
else:
231232
axes_names += [None]
@@ -327,8 +328,13 @@ def df_asarray(df, sort_rows=False, sort_columns=False, raw=False, parse_header=
327328
raise ValueError('sort_rows=True is not valid for 1D arrays. Please use sort_columns instead.')
328329
res = from_series(series, sort_rows=sort_columns)
329330
else:
330-
axes_names = [decode(name, 'utf8') if isinstance(name, basestring) else name
331-
for name in df.index.names]
331+
def parse_axis_name(name):
332+
if isinstance(name, basestring):
333+
name = decode(name, 'utf8')
334+
if not name:
335+
name = None
336+
return name
337+
axes_names = [parse_axis_name(name) for name in df.index.names]
332338
unfold_last_axis_name = isinstance(axes_names[-1], basestring) and '\\' in axes_names[-1]
333339
res = from_frame(df, sort_rows=sort_rows, sort_columns=sort_columns, parse_header=parse_header,
334340
unfold_last_axis_name=unfold_last_axis_name, cartesian_prod=cartesian_prod, **kwargs)

larray/tests/test_array.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,11 +4128,22 @@ def test_to_excel_xlwings(tmpdir):
41284128

41294129

41304130
def test_dump():
4131+
# narrow format
41314132
res = list(ndtest(3).dump(wide=False, value_name='data'))
41324133
assert res == [['a', 'data'],
41334134
['a0', 0],
41344135
['a1', 1],
41354136
['a2', 2]]
4137+
# array with an anonymous axis and a wildcard axis
4138+
arr = ndtest((Axis('a0,a1'), Axis(2, 'b')))
4139+
res = arr.dump()
4140+
assert res == [['\\b', 0, 1],
4141+
['a0', 0, 1],
4142+
['a1', 2, 3]]
4143+
res = arr.dump(_axes_display_names=True)
4144+
assert res == [['{0}\\b*', 0, 1],
4145+
['a0', 0, 1],
4146+
['a1', 2, 3]]
41364147

41374148

41384149
@needs_xlwings
@@ -4293,7 +4304,7 @@ def test_open_excel(tmpdir):
42934304
assert_array_equal(res, a3.data.reshape((6, 4)))
42944305

42954306
# 4) Blank cells
4296-
# ========================
4307+
# ==============
42974308
# Excel sheet with blank cells on right/bottom border of the array to read
42984309
fpath = inputpath('test_blank_cells.xlsx')
42994310
with open_excel(fpath) as wb:
@@ -4309,7 +4320,16 @@ def test_open_excel(tmpdir):
43094320
assert_array_equal(bad3, good2)
43104321
assert_array_equal(bad4, good2)
43114322

4312-
# 5) crash test
4323+
# 5) anonymous and wilcard axes
4324+
# =============================
4325+
arr = ndtest((Axis('a0,a1'), Axis(2, 'b')))
4326+
fpath = tmp_path(tmpdir, 'anonymous_and_wildcard_axes.xlsx')
4327+
with open_excel(fpath, overwrite_file=True) as wb:
4328+
wb[0] = arr.dump()
4329+
res = wb[0].load()
4330+
assert arr.equals(res)
4331+
4332+
# 6) crash test
43134333
# =============
43144334
arr = ndtest((2, 2))
43154335
fpath = tmp_path(tmpdir, 'temporary_test_file.xlsx')

0 commit comments

Comments
 (0)