56 points · by garren · 5y ago · 68 comments
Wes wrote a 10 things I hate about Pandas article back in 2017 that said "pandas rule of thumb: have 5 to 10 times as much RAM as the size of your dataset". Source: https://wesmckinney.com/blog/apache-arrow-pandas-internals/
I'm not sure that rule of thumb is up-to-date anymore. Modern versions of Pandas use PyArrow, another project Wes started, and should be more memory efficient.
Wes just started a new company that'll continue building Arrow: https://medium.com/@patterson.joshua/joining-forces-for-an-a...
Dask makes it easy to scale Pandas, a la Spark. Each partition in a Dask DataFrame is a Pandas DataFrame, so you can indirectly use Pandas on terabytes of data. Disclosure: I work at Coiled, the Dask company founded by Matt Rocklin.
The PyData stack makes machine learning / advanced analytics so much easier than Spark in my opinion. I still love Spark for ETL, but think modern PyData is a better stack for ML (for most companies).
def input_file_name():
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.input_file_name())
You can see it's just using py4j to invoke a JVM function.Pandas is the swiss army knife of data science prep work. There are very few things it can't do. But to achieve that flexibility, it has a very complex API. That can be confusing when starting out. A lot of beginner work feels like just memorizing arbitrary recipes. Once you understand it, it becomes much more powerful. When I first started, the gratuitous use of nested square brackets drove me crazy. Now I understand what's happening beneath the hood, and I use that expressive power on a daily basis.
The critiques of memory usage are well founded and hopefully the underlying tech implementations continue to improve over time.
It's funny, I have no problem understanding basic syntax of numpy, but when I meet a DataFrame, my brain just can't.
But I could imagine that, if I were about to do something quite different from things I've done before, that reading the relevant chapter in the book. For example, of course I've dealt with dates and times using Pandas. But I haven't used time spans. Instead of googling when I get stuck, I could read section 2.20.
e.g. if you don't know about pandas.Series.str then you probably wouldn't find df['colname'].str.upper just by hitting tab. So, yes good to have Google and also an offline manual that has approximately everything.
To your point, people did have a “programming language” for databases and it was called CODASYL. It died in the 1970s in favor to the relational model. NoSQL and other recent trends have similarly been criticized pretty heavily compared to relational databases.
I guess the point is that SQL and friends may feel restrictive but alternatives sacrifice on performance or safety.
Spoiler alert: it's the former.
Does some stuff get complicated? Sure. Sometimes there are edge cases that get messy. The other day I was surprised to see my categorical labels implicitly converted to strings if they were used in a multi-level index. But for me, at least, that’s a fairly esoteric use-case. It stands out to me now precisely because pandas so rarely does something I didn’t expect.
One funny thing, it's only in the Python world that C is a defined speed. When we implement libraries in actual C, C++, Rust etc, we benchmark and compare different libraries, they can have wildly different performance - depending on you know, work put into them. But Python is just a two-speed world: Python or C ;-)
Like this standard usage which misuses all the operators:
a = b[c == 1]
One would think that would be equivalent to b[true] or false, and what's that supoosed to mean? But if c is a numpy array it's instead doing element wise comparison on c, and the new array of booleans will be used to index into b. If you don't know this, it's impossible to figure out looking at the code. Too many footguns imo. Same with pytorch, broadcasting is nice, but makes it hard to figure out what's going to happen in practice with the shapes being calculated upon.A more weird but still basic example from pandas is the way it handles indexing and the confusion between .loc, .iloc, .ix, .at and plain '[]'. That part of the API should be really basic but for some reason it's a giant mess.
I feel Notebooks aren't used as much because they're a great tool, but that they are used so much as a necessity since you need to look at live data to have any hopes of understanding what's going on. Why couldn't a compiler instead tell me what I'm trying to do will fail?
The array syntax is not what you expect if you're coming from a background primarily in programming applications using other dynamically-typed languages or even Python itself, but it's exactly what you'd expect if you're a scientist used to using MATLAB, or even just used to doing math on paper. And at least back in the early days, that was how hedge funds and quantitative finance worked. They weren't looking for application programmers. They were looking for physicists and electrical engineers. It was a lot easier to teach them how to program than to teach programmers about Brownian motion and stochastic processes.
For their intended use cases, Numpy and pandas are among the greatest tools ever created, honestly comparable to MATLAB itself except also free.
What you've shown here is pandas 101. If it is confusing, it's a sign you need to understand the underlying basics of NumPy and pandas.
Pandas is powerful, but I can never get a command to work on the first try.
And there's gotchas like join() not actually working like you'd expect a join to coming from SQL. IIRC a SQL-like join is done via merge().
And then I still don't understand when I need to differentiate between indexes and columns. I don't even know why they need to be differentiated at all. Would it really have been that hard for them to design indexes to always project out to a column so you can treat them as such?
Apart from using xarray whenever feasible, but that's just one small part (and always in conjunction with pandas).
If you think that these complaints are not valid or stem from a lack of experience, I urge you to use R first.
(I’ve heard tell that R is worse, but I wouldn’t have any opinion on that since I’ve never used the language.)
So it stands to reason there must be a common explanation whey they are all so obtuse, which I would venture is something like: they solve a set of problems so broad and complex that the tools themselves must also be massively broad and complex, and so the only way to avoid API inconsistencies is to have the system designed whole-cloth from the very beginning, but doing so would make the development process so huge that it would take a decade before you had something ready for users and so it would never get off the ground. The tools that succeed in reality are the ones that solve a common subset of problems, and then grow organically as they are extended to solve other subsets, and the varying subsets have logical inconsistencies and conflicts, thus the state of tools available today having APIs with inconsistencies and conflicts.
That said, pandas is really good at solving problems with tabular data, and even though it's not easy to do complex work with I still like and appreciate it very much.
The alternative really is to write simple imperative style scripts with for loops in your favorite scripting language. Script will be much longer and likely more prone to errors, but the learning curve for simple tasks is just a lot less steep.
Eventually learning the array oriented approach is worth it though. It's the usual trade off in how much time is it worth learning a new tool vs kludging some old tool that doesn't quite fit for the task.
Take this with a grain of salt from someone who needs data manipulation occasionally every now and then (as opposed to being a full time number-cruncher, data-scientist, statistician etc.), using krangl[1] for Kotlin has been a great experience.
I was drawn to this library because I use Kotlin in my dayjob for backend development, but I love how well Kotlin's succinct syntax & features like extension functions lends itself to data transformation & ETL kind of use cases.
Also it doesn't hurt that JVM has a plethora of libraries available for things like DB access, plotting, etc.
I am sure that Pandas has many features I am unaware of, and for a lot of people the high-ish startup time can be a deterrant, but for most of my day to day data munging the combination of jbang, krangl & kravis has been a pretty good fit.
Lots of GUI based alternatives for data munging such as: Easy Data Transform, Alteryx, Knime. Whether they are better than text based tools, such as R or Python+Pandas, depends a lot on your use case and experience.
Data Science happened to python largely against its will (ie., that of GvR) -- and language-level support simply doesn't exist.
It can be both, but it doesn’t have to.
Python is an imperative, object-oriented language largely concerned with side-effecting programs (web severs, os automation, etc.).
It is about as hostile as it is possible to be, from "ideas" pov, to data science.
However those ideas are just a syntax skin over something much more flexible: pointers and dictionaries (/hashmaps). These are like the "Python VM Language" on which "Data Science" sits.
Data Science libraries essentially use the "actual" semantics of python and ignore the "official" syntax, as that's the only way of providing an API that makes any sense. (Ie., isnt just reams of for-loops with 5 sequential obj.method() calls).
I don't believe there is a "non-confusing" way of ignore python's syntax, *within python*; and that's just what is required.
[1] I have never failed to find an answer for Pandas on SO for even the most convoluted manipulations I'm attempting. The first hit I look at usually has my answer.
It is certainly interesting though, how there are so many people firmly in opposite camps.
I really cannot imagine how pandas could be considered well put together; I'm always searching stackoverflow for data transformations I assume should be common and simple.
In dplyr, on the other hand, each function does one and only one thing (the touted Unix principle) and you just pass the data along via the pipe %>% operator