Skip to content

Commit 31272f2

Browse files
committed
fixed from_series(sort_rows=False)
added test for from_series
1 parent 26aa305 commit 31272f2

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

larray/inout/array.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ def from_series(s, sort_rows=False):
8282
s : Pandas Series
8383
Input Pandas Series.
8484
sort_rows : bool, optional
85-
Whether or not to sort the rows alphabetically (sorting is more efficient than not sorting).
86-
Defaults to False.
85+
Whether or not to sort the rows alphabetically. Defaults to False.
8786
8887
Returns
8988
-------
@@ -92,9 +91,9 @@ def from_series(s, sort_rows=False):
9291
name = s.name if s.name is not None else s.index.name
9392
if name is not None:
9493
name = str(name)
95-
labels = sorted(s.index.values) if sort_rows else list(s.index.values)
96-
axis = Axis(labels, name)
97-
return LArray(s.values, axis)
94+
if sort_rows:
95+
s = s.sort_index()
96+
return LArray(s.values, Axis(s.index.values, name))
9897

9998

10099
def from_frame(df, sort_rows=False, sort_columns=False, parse_header=True, unfold_last_axis_name=False, **kwargs):

larray/tests/test_array.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from larray import (LArray, Axis, LGroup, union, zeros, zeros_like, ndrange, ndtest, ones, eye, diag, stack,
1818
clip, exp, where, X, mean, isnan, round, read_hdf, read_csv, read_eurostat, read_excel,
1919
from_lists, from_string, open_excel, from_frame, sequence, nan_equal)
20+
from larray.inout.array import from_series
2021
from larray.core.axis import _to_ticks, _to_key
2122
from larray.util.misc import StringIO
2223

@@ -2680,6 +2681,17 @@ def test_from_lists(self):
26802681
['F', 'FO', 0, 0, 2]], sort_columns=True)
26812682
assert_array_equal(sorted_arr, arr)
26822683

2684+
def test_from_series(self):
2685+
expected = ndtest(3)
2686+
s = pd.Series([0, 1, 2], index=pd.Index(['a0', 'a1', 'a2'], name='a'))
2687+
assert_array_equal(from_series(s), expected)
2688+
2689+
s = pd.Series([2, 0, 1], index=pd.Index(['a2', 'a0', 'a1'], name='a'))
2690+
assert_array_equal(from_series(s, sort_rows=True), expected)
2691+
2692+
expected = ndtest(3)[['a2', 'a0', 'a1']]
2693+
assert_array_equal(from_series(s), expected)
2694+
26832695
def test_from_frame(self):
26842696
# 1) data = scalar
26852697
# ================

0 commit comments

Comments
 (0)