We see many questions each week, often multiple questions a day, asking to use a library / tool / language / feature in a non-recommended way. According to this Meta SE answer, "Don't Do It" with an explanation is a valid answer.
Specific case
I'm focusing on pandas to demonstrate the problem, but the general issue may be applicable to many tags. There's a current trend "because it's possible" to put lists in pandas columns / series. Here are some recent questions:
- Python Pandas: multiple aggregations -> list of values
- Assigning to pandas DataFrame column behaves differently depending on other columns
- Pandas- How to check if list of strings in DF row contains any of strings in series in another DF?
- Summing multiple lists stored in dataframe
- Adding lists stored in dataframe
Note: These questions are not duplicates, but have a bad premise / starting point / workaround. So canonical posts will not help here.
There are hundreds, possibly thousands, of these questions scattered across SO. In my opinion, each and every one of these can justifiably be answered with:
Don't do this. Pandas was never designed to hold lists in series / columns. You can concoct expensive workarounds, but these are not recommended.
The main reason holding lists in series is not recommended is you lose the vectorised functionality which goes with using NumPy arrays held in contiguous memory blocks. Your series will be of
object
dtype, which represents a sequence of pointers, much likelist
. You will lose benefits in terms of memory and performance, as well as access to optimized Pandas methods.See also What are the advantages of NumPy over regular Python lists? The arguments in favour of Pandas are the same as for NumPy.
The problem
I fear plastering this on every such question won't go down well, even though it's exactly what I want to say, and nothing more, i.e. I purposely want to avoid getting into the details of the question because it adds noise to the answer.
Furthermore, such duplicated answers will come up as "duplicated answer" in moderator tools and I expect repercussions.
So what can I do?
A comment is ephemeral. Even with a link, it does not do justice to the extent of the problem / issue. The generic "Don't Do This" answer is likely to cause trouble, though I believe professional or enthusiast programmers will find it helpful.
Before the alarm bell sounds, I'm not looking for rep out of this, more than happy to community-wiki such answers.
Here's what I have, here's what I want
. The bit theyhave
though is bad practice and not recommended. Often, as here, it stems from an XY problem.However, an answer that tells the poster how to do something stupid, just to strictly answer the poster's question, is worse than useless.
I put this in the same category. But, in this case, we needn't worry. There's no shortage of[pandas]
followers willing to answer these questions, I can simply addIf you wish to go ahead, use @abc's answer
to make it "unique".pandas
cells are never appropriate; it's exactly the same situation as storing blobs in a database (or blobs inpandas
, for that matter). Sure, there's often a more machine-efficient solution, but potentially at the cost of some other consideration (memory, atomicity, etc.). For this Meta question, I fear that creating a hammer like this just encourages people to swing it whenever they see what they think is a matching nail, without trying to understand the actual need of the question.