Skip to content

Commit 78bb1bf

Browse files
alixdammangdementen
authored andcommitted
Update from_string function. dtype is automatically inferred (fixes #131)
1 parent 7a97225 commit 78bb1bf

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

larray/core.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@
112112
from larray.utils import (table2str, size2str, unique, csv_open, unzip, long,
113113
decode, basestring, unicode, bytes, izip, rproduct,
114114
ReprString, duplicates, array_lookup2, strip_rows,
115-
skip_comment_cells, find_closing_chr, PY3)
115+
skip_comment_cells, find_closing_chr, StringIO, PY3)
116+
116117

117118
def _range_to_slice(seq, length=None):
118119
"""
@@ -9165,7 +9166,7 @@ def from_lists(data, nb_index=None, index_col=None):
91659166
return df_aslarray(df, raw=index_col is None, parse_header=False)
91669167

91679168

9168-
def from_string(s, nb_index=None, index_col=None, sep=','):
9169+
def from_string(s, nb_index=None, index_col=None, sep=',', **kwargs):
91699170
"""Create an array from a multi-line string.
91709171
91719172
Parameters
@@ -9179,14 +9180,18 @@ def from_string(s, nb_index=None, index_col=None, sep=','):
91799180
List of columns for the index (ex. [0, 1, 2, 3]). Defaults to None (see nb_index above).
91809181
sep : str
91819182
delimiter used to split each line into cells.
9183+
\**kwargs
9184+
See arguments of Pandas read_csv function.
91829185
91839186
Returns
91849187
-------
91859188
LArray
91869189
91879190
Examples
91889191
--------
9189-
9192+
>>> from_string("sex,M,F\\n,0,1")
9193+
sex | M | F
9194+
| 0 | 1
91909195
>>> from_string("nat\\sex,M,F\\nBE,0,1\\nFO,2,3")
91919196
nat\sex | M | F
91929197
BE | 0 | 1
@@ -9200,6 +9205,16 @@ def from_string(s, nb_index=None, index_col=None, sep=','):
92009205
nat\sex | M | F
92019206
BE | 0 | 1
92029207
FO | 2 | 3
9208+
>>> from_string('''age,nat\\sex, M, F
9209+
... 0, BE, 0, 1
9210+
... 0, FO, 2, 3
9211+
... 1, BE, 4, 5
9212+
... 1, FO, 6, 7''')
9213+
age | nat\sex | M | F
9214+
0 | BE | 0 | 1
9215+
0 | FO | 2 | 3
9216+
1 | BE | 4 | 5
9217+
1 | FO | 6 | 7
92039218
92049219
Empty lines at the beginning or end are ignored, so one can also format the string like this:
92059220
@@ -9212,9 +9227,8 @@ def from_string(s, nb_index=None, index_col=None, sep=','):
92129227
BE | 0 | 1
92139228
FO | 2 | 3
92149229
"""
9215-
data = [[cell.strip() for cell in line.split(sep)]
9216-
for line in s.strip().splitlines()]
9217-
return from_lists(data, nb_index=nb_index, index_col=index_col)
9230+
9231+
return read_csv(StringIO(s), nb_index=nb_index, index_col=index_col, sep=sep, skipinitialspace=True, **kwargs)
92189232

92199233

92209234
def read_csv(filepath, nb_index=None, index_col=None, sep=',', headersep=None, na=np.nan,

larray/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import numpy as np
2121

22-
if sys.version < '3':
22+
if sys.version_info[0] < 3:
2323
basestring = basestring
2424
bytes = str
2525
unicode = unicode
@@ -33,6 +33,11 @@
3333
long = int
3434
PY3 = True
3535

36+
if PY3:
37+
from io import StringIO
38+
else:
39+
from StringIO import StringIO
40+
3641

3742
def csv_open(filename, mode='r'):
3843
assert 'b' not in mode and 't' not in mode

0 commit comments

Comments
 (0)