Skip to content

Commit 26aa305

Browse files
committed
fix #495: fixed reading 1D arrays with non-string labels
* added test for reading a csv file from a StringIO object * simplified some test files: test1d.csv, test2d.csv and test3d.csv
1 parent 4535125 commit 26aa305

File tree

6 files changed

+29
-33
lines changed

6 files changed

+29
-33
lines changed

doc/source/changes/version_0_26_1.rst.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ Fixes
1111
1212
* fixed LArray.split_axes using a custom separator and not using sort=True or when the split labels are
1313
ambiguous with labels from other axes (closes :issue:`485`).
14+
15+
* fixed reading 1D arrays with non-string labels (closes :issue:`495`).

larray/inout/array.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ def df_aslarray(df, sort_rows=False, sort_columns=False, raw=False, parse_header
234234
# handle 1D
235235
if len(df) == 1 and (pd.isnull(df.index.values[0]) or
236236
(isinstance(df.index.values[0], basestring) and df.index.values[0].strip() == '')):
237+
if parse_header:
238+
df.columns = pd.Index([parse(cell) for cell in df.columns.values], name=df.columns.name)
237239
series = df.iloc[0]
238240
series.name = df.index.name
239241
return from_series(series, sort_rows=sort_rows)

larray/tests/test1d.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
time,2007,2010,2013
2-
,3722,3395,3347
2+
,0,1,2

larray/tests/test2d.csv

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
age\time,2007,2010,2013
2-
0,3722,3395,3347
3-
1,338,316,323
4-
2,2878,2791,2822
5-
3,1121,1037,976
6-
4,4073,4161,4429
1+
a\b,0,1,2
2+
0,0,1,2
3+
1,3,4,5

larray/tests/test3d.csv

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
age,sex\time,2007,2010,2013
2-
0,F,3722,3395,3347
3-
0,H,338,316,323
4-
1,F,2878,2791,2822
5-
1,H,1121,1037,976
6-
2,F,4073,4161,4429
7-
2,H,1561,1463,1467
8-
3,F,3507,3741,3366
9-
3,H,2052,2052,2118
10-
4,F,4807,4868,4852
11-
4,H,3785,3508,3172
1+
age,sex\time,2015,2016,2017
2+
0,F,0.5,1.5,2.5
3+
0,M,3.5,4.5,5.5
4+
1,F,6.5,7.5,8.5
5+
1,M,9.5,10.5,11.5
6+
2,F,12.5,13.5,14.5
7+
2,M,15.5,16.5,17.5
8+
3,F,18.5,19.5,20.5
9+
3,M,21.5,22.5,23.5

larray/tests/test_array.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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)
2020
from larray.core.axis import _to_ticks, _to_key
21+
from larray.util.misc import StringIO
2122

2223

2324
class TestValueStrings(TestCase):
@@ -2455,23 +2456,15 @@ def test_from_string(self):
24552456
assert_array_equal(res, expected)
24562457

24572458
def test_read_csv(self):
2458-
la = read_csv(abspath('test1d.csv'))
2459-
self.assertEqual(la.ndim, 1)
2460-
self.assertEqual(la.shape, (3,))
2461-
self.assertEqual(la.axes.names, ['time'])
2462-
assert_array_equal(la, [3722, 3395, 3347])
2459+
res = read_csv(abspath('test1d.csv'))
2460+
assert_array_equal(res, ndrange('time=2007,2010,2013'))
24632461

2464-
la = read_csv(abspath('test2d.csv'))
2465-
self.assertEqual(la.ndim, 2)
2466-
self.assertEqual(la.shape, (5, 3))
2467-
self.assertEqual(la.axes.names, ['age', 'time'])
2468-
assert_array_equal(la[0, :], [3722, 3395, 3347])
2462+
res = read_csv(abspath('test2d.csv'))
2463+
assert_array_equal(res, ndrange('a=0,1;b=0,1,2'))
24692464

2470-
la = read_csv(abspath('test3d.csv'))
2471-
self.assertEqual(la.ndim, 3)
2472-
self.assertEqual(la.shape, (5, 2, 3))
2473-
self.assertEqual(la.axes.names, ['age', 'sex', 'time'])
2474-
assert_array_equal(la[0, 'F', :], [3722, 3395, 3347])
2465+
res = read_csv(abspath('test3d.csv'))
2466+
expected = ndrange('age=0..3;sex=F,M;time=2015..2017') + 0.5
2467+
assert_array_equal(res, expected)
24752468

24762469
la = read_csv(abspath('test5d.csv'))
24772470
self.assertEqual(la.ndim, 5)
@@ -2499,6 +2492,10 @@ def test_read_csv(self):
24992492
assert_array_equal(la[X.arr[1], 0, 'F', X.nat[1], :],
25002493
[3722, 3395, 3347])
25012494

2495+
# test StringIO
2496+
res = read_csv(StringIO('a,1,2\n,0,1\n'))
2497+
assert_array_equal(res, ndrange('a=1,2'))
2498+
25022499
def test_read_eurostat(self):
25032500
la = read_eurostat(abspath('test5d_eurostat.csv'))
25042501
self.assertEqual(la.ndim, 5)

0 commit comments

Comments
 (0)