Pandas Codebase.
There is a saying that goes “To be a good writer, you have to be a good reader”, same goes to programming; to write better codes, you have to read codes. The past week I was tasked with reading a script and explain what the code does, you can read here. I then took it a step further by reading pandas codebase, and I must say, I have a lot to say. At first, everything looked super confusing. So, I started by focusing on parts of the codebase that felt familiar. That made it easier to navigate and understand what was going on.
While I haven’t finished reading the entire codebase, I’d love to share some of the things I’ve learned along the way:
1. Pandas as a package:
One of the first things I noticed when I started reading the Pandas codebase was this:
“Pandas is a package, so why does VSCode IntelliSense say it’s a module?”
That question led me to do some digging, and here’s what I found. When you run an import statement in Python, the interpreter goes through the directory structure to figure out whether it’s dealing with a package, subpackage, or module. What confirms that a directory is a package is the presence of a special file called __init__.py.
Think of __init__.py as the door to a building. It’s presence signals that there's an entire structure (the package) behind it, and it’s also the entry point to access what’s inside. Every package or subpackage in Python must contain an __init__.py file. This file plays a crucial role by making other components like subpackages, modules, classes, functions, and methods accessible. When you import a package, Python actually runs the content of it’s __init__.py file.
This is why tools like VSCode might display it as a module: because what’s being executed and what gives access to the rest is that special file.
2. Raise:
raise is used to intentionally trigger an exception in your code. It stops the program immediately and throws an error, even if the code could have continued. This is useful when you want to enforce a rule or warn the user that something isn't allowed. It’s different from try-except, where you handle errors that might happen unexpectedly. For example:
def raise_error(a, b):
if b == 0:
raise ZeroDivisionError("Number cannot be divisible by 0")
else:
division = a/b
return divisionIn the code above, If b == 0, we raise a ZeroDivisionError on purpose with a message: "Number cannot be divisible by 0". Otherwise, it divides a by b and returns the result.
3. Error Handling:
Okay, so I’ve been using try and except to handle errors and that has been it. While reading the Pandas codebase, I realized there’s more to error handling than I initially thought, I discovered you can actually use a try block inside an except block. At first, this looked strange, but it makes sense when you think of it as giving your code a second chance before completely giving up.
Here’s a simple example to illustrate:
try:
# Try to open the primary file location
except FileNotFoundError:
try:
# If that fails, try opening from a backup location
except FileNotFoundError:
# If that also fails, then raise an exceptionWhile continuing through the codebase, I also came across another structure that used else and finally along with try and except. I looked it up, and here’s how they work:
try:
# Run this block of code
except Exception:
# Run this if an exception is raised
else:
# Run this only if no exception occurred in the try block
finally:
# This block will run no matter what — whether there was an exception or not It’s important to note that you can mix and match these blocks to suit the specific needs of your code. You don’t always need to use all of them, just the ones that make sense for your particular use case.
4. Helper Functions/Classes:
Helper functions/classes are small, reusable functions that perform specific tasks and are usually used within larger functions or programs to keep the code clean, organized, and readable. They are not the “main” logic, but they support it, hence, the name “helper” Think of them as assistants that handle smaller jobs so that your main code doesn’t get too cluttered.
Funny enough, I’ve come across helper functions and classes so many times, I’ve even written them myself. But it wasn’t until when i was reading pandas codebase that I realized, “Ohhh… so this is what they’re actually called”
This might seem like a small or irrelevant detail, but trust me, you don’t want to be in a situation where you’re using something regularly and then hear it mentioned, only to look completely clueless because you didn’t know that’s what it is called.
Here is an illustration of how it is written for better understanding:
def add_two_numbers(a, b):
return a + b
def main():
result = add_two_numbers(5, 3)
print("The sum is:", result)
main()add_two_numbers()is the helper function, it just does one job, adds two numbersmain()is the main logic, it uses the helper to keep things clean and simple
5. Special variable __all__:
The __all__ variable is a special variable that you’ll often find in the __init__.py file, the same special file we talked about earlier. It comes into play when you use an import statement like this:
from package_name import *Now, this kind of import (import *) is generally discouraged because it can lead to unexpected behavior, especially when you're not sure what exactly is being imported.
Get Taibat Abowaba’s stories in your inbox
Join Medium for free to get updates from this writer.
This is where __all__ becomes useful. It acts as a whitelist, explicitly defining which functions, classes, variables, modules, or subpackages are safe to expose when import * is used.
Even if there are other items in the package, anything not listed in __all__ won’t be imported, including non-public elements (which I’ll explain in the next section). Here’s a quick demonstration of how __all__ is written in code:
def calculate_sum(a, b):
return a + b
class DataCleaner:
pass
def _helper_function():
pass
__all__ = ['calculate_sum', 'DataCleaner']In this example:
- Only
calculate_sumandDataCleanerwill be imported if someone usesfrom module_name import * _helper_functionis not included in__all__, so it will be ignored even though it exists in the file
6. Single Underscore Prefix vs Double Underscore Prefix:
You’ve probably come across a module, class, variable, method, or function that starts with a single underscore (_) and maybe just ignored it, not realizing that it’s actually trying to communicate something important. Well, I’m glad to explain what that is and why it matters.
A single underscore prefix is used to indicate that an element is non-public. It’s basically a gentle hint to developers:
“Hey, this part of the code is meant for internal use, please don’t touch it unless you know what you’re doing”
It doesn’t enforce any restrictions (you can still access it), but it signals that the element might change, behave differently, or be unstable if used outside its intended context.
Now, a double underscore prefix (__) is a bit different. While a single underscore is just a convention, a double underscore actually triggers name mangling in Python. What does that mean? Python changes the name of the variable or method behind the scenes to make it harder to access from outside the class. It’s not bulletproof privacy, but it’s Python’s way of saying:
“This is very private, don’t access it directly”
Here’s a simple example to explain the difference:
class Example:
def __init__(self):
self.public = "I’m public"
self._semi_private = "I’m non-public (just a hint)"
self.__very_private = "I’m name-mangled"
obj = Example()
print(obj.public) # Accessible
print(obj._semi_private) # Accessible, but meant to be internal
print(obj.__very_private) # Will raise AttributeErrorTrying to access __very_private directly will raise an error. But behind the scenes, Python renames it like this:
print(obj._Example__very_private) # This worksName mangling becomes especially useful in the context of inheritance. It helps avoid name clashes between a parent class and its subclasses and makes private attributes harder to accidentally override or access.
When you use a double underscore prefix (like __balance) in a class, Python name-mangles it, meaning it renames the attribute internally (e.g., to _ClassName__balance). This prevents subclasses from unintentionally modifying or overriding it.
Why Name Mangling Matters
In inheritance, a subclass might define a method or variable using the same name as one in the parent class. Without name mangling, this could lead to naming conflicts and bugs that are hard to trace.
With name mangling:
- The base class keeps its internal logic protected.
- Subclasses can define their own variables with the same names without interfering with the parent class.
- This adds a layer of safety to your code, especially in large or collaborative projects.
If you found this helpful and learnt one or two things, do well to leave like and leave a comment. You can also connect with me on LinkedIn
#dataengineering #data #pandas #codebase










