How to enable REFS_OK flag in nditer in numpy in Python 3.3?

Does anyone know how to enable the REFS_OK flag in numpy? I can't seem to find a clear explanation on the Internet.

My code is:

import sys import string import numpy as np import pandas as pd SNP_df = pd.read_csv('SNPs.txt',sep='\t',index_col = None ,header = None,nrows = 101) output = open('100 SNPs.fa','a') for i in SNP_df: data = SNP_df[i] data = np.array(data) for j in np.nditer(data): if j == 0: output.write(("\n>%s\n")%(str(data(j)))) else: output.write(data(j)) 

I keep getting an error: the Iterator operand or the requested dtype contains references, but REFS_OK was not included.

I cannot decide how to enable the REFS_OK flag so that the program can continue ...

+4
source share
2 answers

I highlighted the problem. No need to use np.nditer. The main problem was that I misinterpreted how Python would read iterator variables in a for loop. Adjusted code below.

 import sys import string import fileinput import numpy as np SNP_df = pd.read_csv('datafile.txt',sep='\t',index_col = None ,header = None,nrows = 5000) output = open('outputFile.fa','a') for i in range(1,51): data = SNP_df[i] data = np.array(data) for j in range(0,1): output.write(("\n>%s\n")%(str(data[j]))) for k in range(1,len(data)): output.write(str(data[k])) 
+2
source

If you really want to enable the flag, I have a working example.

Python 2.7, numpy 1.14.2, pandas 0.22.0

 import pandas as pd import numpy as np # get all data as panda DataFrame data = pd.read_csv("./monthdata.csv") print(data) # get values as numpy array data_ar = data.values # numpy.ndarray, every element is a row for row in data_ar: print(row) sum = 0 count = 0 for month in np.nditer(row, flags=["refs_OK"], op_flags=["readwrite"]): print month 
0
source

Source: https://habr.com/ru/post/1479037/


All Articles