I'm wondering if there's any way to change the default display options for pandas. I'd like to change the display formatting as well as the display width each time I run python, eg:
pandas.options.display.width = 150
I see the defaults are hard-coded in pandas.core.config_init
. Is there some way in pandas to do this properly? Or if not, is there some way to set up ipython at least to change the config each time I import pandas? Only thing I can think of is making my own mypandas library that wraps pandas with some extra commands issued each time it's loaded. Any better ideas?
Have a look at the docs:
Using startup scripts for the python/ipython environment to import
pandas and set options makes working with pandas more efficient. To do
this, create a .py or .ipy script in the startup directory of the
desired profile. An example where the startup folder is in a default
ipython profile can be found at:
$IPYTHONDIR/profile_default/startup
More information can be found in the ipython documentation. An example
startup script for pandas is displayed below:
import pandas as pd
pd.set_option('display.max_rows', 999)
pd.set_option('precision', 5)
(or use the new form pd.options.display.max_rows = 999
).
You also asked:
-- is there any way to only run the pandas code when I import pandas from within ipython? it takes quite a while to import pandas, so I'd rather not do it every time I launch a new ipython instance
As a workaround, you could import pandas in the background. See Import python modules in the background in REPL.
As described here, there are iPython config files:
# Most of your config files and extensions will probably start
# with this import
import IPython.ipapi
ip = IPython.ipapi.get()
# You probably want to uncomment this if you did %upgrade -nolegacy
# import ipy_defaults
import os
import pandas
def main():
#ip.dbg.debugmode = True
ip.dbg.debug_stack()
# uncomment if you want to get ipython -p sh behaviour
# without having to use command line switches
import ipy_profile_sh
import jobctrl
# Configure your favourite editor?
# Good idea e.g. for %edit os.path.isfile
#import ipy_editors
# Choose one of these:
#ipy_editors.scite()
#ipy_editors.scite('c:/opt/scite/scite.exe')
#ipy_editors.komodo()
#ipy_editors.idle()
# ... or many others, try 'ipy_editors??' after import to see them
# Or roll your own:
#ipy_editors.install_editor("c:/opt/jed +$line $file")
o = ip.options
# An example on how to set options
#o.autocall = 1
o.system_verbose = 0
#import_all("os sys")
#execf('~/_ipython/ns.py')
# -- prompt
# A different, more compact set of prompts from the default ones, that
# always show your current location in the filesystem:
#o.prompt_in1 = r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Normal\n\C_Green|\#>'
#o.prompt_in2 = r'.\D: '
#o.prompt_out = r'[\#] '
# Try one of these color settings if you can't read the text easily
# autoexec is a list of IPython commands to execute on startup
#o.autoexec.append('%colors LightBG')
#o.autoexec.append('%colors NoColor')
o.autoexec.append('%colors Linux')
pandas.options.display.width = 150
# some config helper functions you can use
def import_all(modules):
""" Usage: import_all("os sys") """
for m in modules.split():
ip.ex("from %s import *" % m)
def execf(fname):
""" Execute a file in user namespace """
ip.ex('execfile("%s")' % os.path.expanduser(fname))
main()
Probably better to make separate Python profiles. (The code is untested).
I was able to fix this by actually going into the pandas folder (found using pandas.__file__
). Inside the pandas folder there is a core folder with the config_init.py
file. The lines
cf.register_option('large_repr', 'truncate', pc_large_repr_doc,
validator=is_one_of_factory(['truncate', 'info']))
set the default options. So you can change the second argument to 'info'
cf.register_option('large_repr', 'info', pc_large_repr_doc,
validator=is_one_of_factory(['truncate', 'info']))
and then by default pandas will print summary table if the data frame exceeds max_rows
or max_columns
, which you can also change the default for in this file. I'm not sure if this is safe behavior, but it worked for me.