r/IPython • • Feb 28 '15

What exactly is IPython?

Hello,

I am relatively new to learning Python, and am confused by exactly what IPython is. Is it an implementation of Python like CPython, Jython, PyPy et al.? Or is it more like an IDE like Eric or Spyder? Or is it an IDE that is integrated with its own implementation of Python? Would the Notebook be part of that IDE? Is there an ELI5 explanation for how to conceptualise IPython?

Thanks!!

15 Upvotes

20 comments sorted by

View all comments

Show parent comment

3

u/hharison Mar 01 '15

It's worth also mentioning that the basic idea has been expanded on and you can use the interactive shells to interact with backends in different languages, notably Julia and R. The general project is called Jupyter. So this another big advantage of IPython---in one notebook you can run Python code and R code, for example.

2

u/avamk Mar 01 '15

Wow that sounds really cool! Since I know a teeny bit of R here's an important question:

In an IPython notebook, can the Python and R code both act on and use the same data and share stuff between them??? If so wouldn't that be great and you get the best of both worlds?

2

u/MeshachBlue Mar 01 '15

Yup, definitely can. You can make an "R cell" with cell magic. Let me know if you want help making this happen.

2

u/[deleted] Mar 01 '15

Yes with rpy2. To send data from R to Python you can use the -o flag per this documentation.

2

u/hharison Mar 01 '15

Yes, and yes it is awesome. I do it all the time, since I like to do my data-munging in Python but I often am using statistical methods that no one's ported to Python yet.

It all works through rpy2. You can use rpy2 methods directly, and they're also exposed in IPython's "R magic" commands as well as in pandas through some DataFrame methods.

1

u/avamk Mar 01 '15

Woah... R "magic", "pandas" (???), DataFrame methods, wha?? Are they Python libraries or something?

3

u/hharison Mar 02 '15

IPython magic are special commands that you can run in IPython that start with %. These include commands like %R which is colloquially referred to as "R magic". It uses rpy2 under the hood to convert objects and run R code. Check out the IPython docs.

(Methods are functions attached to objects)

Pandas is a python library that provides an object DataFrame which is similar to R's data.frame. There is a method DataFrame.to_r_dataframe (forget the exact name, am on my phone so can't quickly look it up) which converts it to R using rpy2.

So basically there are three ways to do the same thing:

  1. Use rpy2 to convert objects between python and R, and run R code passed as strings.
  2. Use IPython's R magic %R magic to run a line of code in R (there is also %%R in the notebook to run a whole cell in R, and magic to convert objects back and forth).
  3. Use pandas.DataFrame.to_r or whatever to convert a DataFrame to R. (You will need to use one of the above techniques to then operate on it).

Read the rpy2/IPython/pandas docs for details.

1

u/avamk Mar 02 '15

Sounds great, I will definitely check out the documentation as I learn all this stuff.

Thank you for your detailed reponses!