Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Security Violation on using Pandas set_option for precision. Any work arounds?

I get the following Security Violation message when trying to set the precision of my pandas' data frame outputs. Does anyone have a workaround for this?

The intent is to shorten the significant digits being displayed on the screen when a pandas data frame is recalled. I want to preserve the data in its full value, so I have opted to not round down the values for the sake of a quick solution, I just want the display to be easier to see (3 significant digits).

---------------------------------------------------------------------------  
SecurityViolation                         Traceback (most recent call last)  
<ipython-input-4-30a02ddbcc31> in <module>()  
----> 1 pd.set_option('precision', 3)

<quantopian> 

SecurityViolation: 0002 Security Violation(s): Accessing pd.set_option raised an AttributeError. No attributes with a similar name were found.  
2 responses

You can apply a format to a column by using the apply method. This won't change the underlying value, only how it's displayed. Like this

df['my_number_column'] = df['my_number_column'].apply("{0:.2f}".format)

This also works nice for formatting dates

df['my_date_column'] = df['my_date_column'].apply("{:%d, %b %Y}".format)

This approach formats the individual values. If new rows are added after formatting, they will not automatically share the same format. For this reason, It's best to execute the apply method just before printing or display. Good luck.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Dan,

Thanks for the solution. Does this change your data frame values to strings? When I check my new values I get a class 'str', originally they are class 'numpy.float64'. So, unfortunately, it does change the underlying value for me.