Skip to content

Commit 2348257

Browse files
committed
(issue 805) updated LArray.to_frame(): call AxisCollection.display_names property at the beginning to to make LArray.to_frame() consistent with LArray.dump() (and then to make it possible to handle anonymous and/or wildcard axes when dealing with CSV and Excel formats)
1 parent 8cf1fc9 commit 2348257

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

larray/core/array.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,22 +1185,21 @@ def to_frame(self, fold_last_axis_name=False, dropna=None):
11851185
b1 6 7
11861186
"""
11871187
columns = pd.Index(self.axes[-1].labels)
1188+
axes_names = self.axes.display_names[:]
11881189
if not fold_last_axis_name:
1189-
columns.name = self.axes[-1].name
1190+
columns.name = axes_names[-1]
11901191
if self.ndim > 1:
1191-
axes_names = self.axes.names[:-1]
1192+
_axes_names = axes_names[:-1]
11921193
if fold_last_axis_name:
1193-
tmp = axes_names[-1] if axes_names[-1] is not None else ''
1194-
if self.axes[-1].name:
1195-
axes_names[-1] = "{}\\{}".format(tmp, self.axes[-1].name)
1194+
_axes_names[-1] = "{}\\{}".format(_axes_names[-1], axes_names[-1])
11961195
if self.ndim == 2:
1197-
index = pd.Index(data=self.axes[0].labels, name=axes_names[0])
1196+
index = pd.Index(data=self.axes[0].labels, name=_axes_names[0])
11981197
else:
1199-
index = pd.MultiIndex.from_product(self.axes.labels[:-1], names=axes_names)
1198+
index = pd.MultiIndex.from_product(self.axes.labels[:-1], names=_axes_names)
12001199
else:
12011200
index = pd.Index([''])
12021201
if fold_last_axis_name:
1203-
index.name = self.axes.names[-1]
1202+
index.name = axes_names[-1]
12041203
data = np.asarray(self).reshape(len(index), len(columns))
12051204
df = pd.DataFrame(data, index, columns)
12061205
if dropna is not None:

larray/tests/test_array.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3454,6 +3454,17 @@ def test_from_series():
34543454
assert_array_equal(res, expected)
34553455

34563456

3457+
def test_to_frame():
3458+
# array containing anonymous axes
3459+
arr = ndtest((Axis(2), Axis(2), Axis(2)))
3460+
df = arr.to_frame()
3461+
assert df.index.name is None
3462+
assert df.index.names == ['{0}*', '{1}*']
3463+
assert df.columns.name == '{2}*'
3464+
assert list(df.index.values) == [(0, 0), (0, 1), (1, 0), (1, 1)]
3465+
assert list(df.columns.values) == [0, 1]
3466+
3467+
34573468
def test_from_frame():
34583469
# 1) data = scalar
34593470
# ================

0 commit comments

Comments
 (0)