python - Numpy accessing column of genfromtxt array raises IndexError -
i'm reading tab separated files. want infer type of columns automatically , read members of first column.
however, type numpy outputs can vary lot, depending on input (single or multi-column) , on dtype parameter used.
here set of test cases
multi_col = "a\t1\t2\nb\t3\t4" = np.genfromtxt(stringio(multi_col), delimiter="\t", skip_header=0, dtype=[('f0', '|s16'), ('f1', '<i8'), ('f2', '<i8')]) print_properties(a, 'a') print("first col a['f0'] = " + str(a['f0']) + '\n') = np.genfromtxt(stringio(multi_col), delimiter="\t", skip_header=0, dtype=none) print_properties(a, 'a') print("first col a['f0'] = " + str(a['f0']) + '\n') = np.genfromtxt(stringio(multi_col), delimiter="\t", skip_header=0, dtype=str) print_properties(a, 'a') print('first col a[:, 0] = ' + str(a[:, 0]) + '\n') single_col = "a\nb" b = np.genfromtxt(stringio(single_col), delimiter="\t", skip_header=0, dtype=[('f0', '|s16')]) print_properties(b, 'b') print("first col b['f0'] = " + str(b['f0']) + '\n') b = np.genfromtxt(stringio(single_col), delimiter="\t", skip_header=0, dtype=none) print_properties(b, 'b') print('already column\n') b = np.genfromtxt(stringio(single_col), delimiter="\t", skip_header=0, dtype=str) print_properties(b, 'b') print('already column\n') single_val = "a\n" c = np.genfromtxt(stringio(single_val), delimiter="\t", skip_header=0, dtype=str) print_properties(c, 'c') print('single value\n') i can't find way access first column in uniform way. different operators needed different cases.
if use col0 = a[:, 0] in first 2 cases, error
indexerror: many indices array on other side, if use col0 = a['f0'], gives error in third case
indexerror: integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`none`) , integer or boolean arrays valid indices is there uniform way access first column without typecasting?
here's solution "worked me".
def print_1st_col(arr): # access first column according different structures numpy can output if arr.dtype.fields not none: print('structured array') col = arr['f0'] # structured array else: if arr.ndim <= 1: print('uniform 1d array') col = arr # direct access (single number or uniform 1d array) else: print('uniform multi-dimensional array') col = arr[:, 0] # uniform multi-dimensional array print(str(col) + '\n')
Comments
Post a Comment