36

I want to print a numpy array without truncation. I have seen other solutions but those don't seem to work.

Here is the code snippet:

total_list = np.array(total_list)
np.set_printoptions(threshold=np.inf)
print(total_list)

And this is what the output looks like:

22        A
23        G
24        C
25        T
26        A
27        A
28        A
29        G
         ..
232272    G
232273    T
232274    G
232275    C
232276    T
232277    C
232278    G
232279    T

This is the entire code. I might be making a mistake in type casting.

import csv
import pandas as pd
import numpy as np



seqs = pd.read_csv('BAP_GBS_BTXv2_imp801.hmp.csv')
plts = pd.read_csv('BAP16_PlotPlan.csv')

required_rows = np.array([7,11,14,19,22,31,35,47,50,55,58,63,66,72,74,79,82,87,90,93,99])
total_list = []


for i in range(len(required_rows)):
    curr_row = required_rows[i];
    print(curr_row)
    for j in range(len(plts.RW)):
        if(curr_row == plts.RW[j]):
            curr_plt = plts.PI[j]
            curr_range = plts.RA1[j]
            curr_plt = curr_plt.replace("_", "").lower()
            if curr_plt in seqs.columns:
                new_item = [curr_row,curr_range,seqs[curr_plt]]
                total_list.append(new_item)
                print(seqs[curr_plt]) 


total_list = np.array(total_list)
'''
np.savetxt("foo.csv", total_list[:,2], delimiter=',',fmt='%s')
total_list[:,2].tofile('seqs.csv',sep=',',format='%s')
'''
np.set_printoptions(threshold='nan')

print(total_list)
7
  • 1
    It works for me, maybe your total_list is not a numpy array? Commented Jun 1, 2017 at 15:33
  • 1
    It is not how numpy arrays look. Are you sure it is not a pandas Series? Commented Jun 1, 2017 at 15:34
  • are you trying to print to a file? Commented Jun 1, 2017 at 15:41
  • That was the actual intent. But had the same problem. So I m trying to print it on the terminal first. Commented Jun 1, 2017 at 15:43
  • does this file need to be human readable? or is it for the purpose of storing the data? Commented Jun 1, 2017 at 15:51

5 Answers 5

34

use the following snippet to get no ellipsis.

import numpy
import sys
numpy.set_printoptions(threshold=sys.maxsize)

EDIT:

If you have a pandas.DataFrame use the following snippet to print your array:

def print_full(x):
    pd.set_option('display.max_rows', len(x))
    print(x)
    pd.reset_option('display.max_rows')

Or you can use the pandas.DataFrame.to_string() method to get the desired result.

EDIT':

An earlier version of this post suggested the option below

numpy.set_printoptions(threshold='nan')

Technically, this might work, however, the numpy documentation specifies int and None as allowed types. Reference: https://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html.

Sign up to request clarification or add additional context in comments.

6 Comments

python and numpy version?
Python 3.5.2, numpy 1.11.1
@Harjatin maybe the other print is not formatted properly (the first one in the loop)
I commented all the other print statements. The output is still truncated.
At least from 1.16.5, this doesn't work: ValueError: threshold must be numeric and non-NAN, try sys.maxsize for untruncated representation, so use sys.maxsize.
|
5

You can get around the weird Numpy repr/print behavior by changing it to a list:

print list(total_list)

should print out your list of 2-element np arrays.

5 Comments

This still gives the same output
Weird - does it happen if you use total_list.tolist() as well?
you probably run a different code what you edit :)) I can confirm both of the answers work fine. add a print("hello") or whatever
What is total_list to start with?
numpy's printing is more useful though for most cases, because it is more readable ― no commas between the numbers..
4

You are not printing numpy arrays.

Add the following line after the imports:

pd.set_option('display.max_rows', 100000)

1 Comment

Thank you. This worked. I am little confused through, shouldn't total_list = np.array(total_list) covert DataFrame to numpy array ?
1
#for a 2d array
def print_full(x):
    dim = x.shape
    pd.set_option('display.max_rows', dim[0])#dim[0] = len(x)
    pd.set_option('display.max_columns', dim[1])
    print(x)
    pd.reset_option('display.max_rows')
    pd.reset_option('display.max_columns')

Comments

1

It appears that as of Python 3, the threshold can no longer be unlimited.

Therefore, the recommended option is:

import numpy
import sys
numpy.set_printoptions(threshold=sys.maxsize)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.